Click here to Skip to main content
15,923,789 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to put range wise data to excel from sql.

My code is below.

C#
System.Data.DataTable dtMainSQLData = new System.Data.DataTable();
da.Fill(dtMainSQLData);
DataColumnCollection dcCollection = dtMainSQLData.Columns;
// Export Data into EXCEL Sheet
Microsoft.Office.Interop.Excel._Application ExcelApp = new Excel.Application();
ExcelApp.Application.Workbooks.Add(Type.Missing);


// ExcelApp.Cells.CopyFromRecordset(objRS);
for (int i = 1; i < dtMainSQLData.Rows.Count + 1; i++)
{

    for (int j = 1; j < dtMainSQLData.Columns.Count + 1; j++)
    {
        if (i == 1)

            ExcelApp.Cells[i, j] = dcCollection[j - 1].ToString();

        else

            ExcelApp.Cells[i, j] = dtMainSQLData.Rows[i - 1][j - 1].ToString();



    }
}
ExcelApp.ActiveWorkbook.SaveCopyAs("E:\\1.xls");
ExcelApp.ActiveWorkbook.Saved = true;
ExcelApp.Quit();
MessageBox.Show("Data Exported Successfully");


[edit]SHOUTING removed, Code block added - OriginalGriff[/edit]
Posted
Updated 29-Jun-14 21:33pm
v2
Comments
OriginalGriff 30-Jun-14 3:33am    
Don't repost: use the "Improve Question" widget to edit your question instead.
I have deleted the original.
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalization if you want to be taken seriously.

I think you should determine a "range" in Excel and insert data to it, it faster than insert cell by cell.

http://www.brad-smith.info/blog/archives/233[^]

About your question, please not just throw code here, tell us WHAT's happened and WHAT do you wanna do. May be this link will help you:

http://www.dotnetperls.com/excel[^]
 
Share this answer
 
C#
using Excel = Microsoft.Office.Interop.Excel;

SaveFileDialog sd = new SaveFileDialog();

sd.Filter = "Excel (*.xls)|*.xls";
sd.DefaultExt = "Excel files (*.xls)|(*.csv)";
sd.Title = "please select your NameA and Path";

if (sd.ShowDialog() == DialogResult.OK)
{

    Excel.Application xlApp;
    Excel.Workbook xlWorkBook;
    Excel.Worksheet xlWorkSheet;
    Excel.Range workSheet_range;
    object misValue = System.Reflection.Missing.Value;

    xlApp = new Excel.ApplicationClass();
    xlApp.Visible = true;
    xlApp.UserControl = true;
    System.Globalization.CultureInfo oldCI=                                    System.Threading.Thread.CurrentThread.CurrentCulture;
    System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
    xlWorkBook = xlApp.Workbooks.Add(1);
    xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

    //Header
    xlWorkSheet.Cells[1, 1] = payGridView.Columns[0].HeaderText;
    xlWorkSheet.Cells[1, 2] = payGridView.Columns[1].HeaderText;
    xlWorkSheet.Cells[1, 3] = payGridView.Columns[3].HeaderText;
    xlWorkSheet.Cells[1, 4] = payGridView.Columns[4].HeaderText;

    workSheet_range = xlWorkSheet.get_Range(xlWorkSheet.Cells[1, 1],
    xlWorkSheet.Cells[1, 4]);
    workSheet_range.Font.Bold = true;


    for (int i = 0; i <= this.payGridView.Rows.Count - 1; i++)
    {
        xlWorkSheet.Cells[i + 2, 1] =  payGridView.Rows[i].Cells[0].Value.ToString();
        xlWorkSheet.Cells[i + 2, 2] = payGridView.Rows[i].Cells[1].Value.ToString();
        xlWorkSheet.Cells[i + 2, 3] = payGridView.Rows[i].Cells[3].Value.ToString();
        xlWorkSheet.Cells[i + 2, 4] =                       payGridView.Rows[i].Cells[4].Value.ToString();

    }

    xlWorkSheet.Rows.AutoFit();
    xlWorkSheet.Columns.AutoFit();

    xlWorkBook.SaveAs(sd.FileName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue,    misValue, misValue, misValue, misValue);
    xlWorkBook.Close(true, misValue, misValue);
    xlApp.Quit();
    System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;

    GlobalClass.releaseObject(xlWorkSheet);
    GlobalClass.releaseObject(xlWorkBook);
    GlobalClass.releaseObject(xlApp);

    MessageBox.Show("Your file is created successfully");
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900