Domanda

I have a working code that exports SQL Server data to Excel.

public static void ExportToExcel(DataTable dt)
{
        // Create sql connection string
        string conString = "Data Source=DELL\\SQLSERVER1;Trusted_Connection=True;DATABASE=Test;CONNECTION RESET=FALSE";
        SqlConnection sqlCon = new SqlConnection(conString);
        sqlCon.Open();

        SqlDataAdapter da = new SqlDataAdapter("select * from tStudent", sqlCon);
        System.Data.DataTable dtMainSQLData = new System.Data.DataTable();
        da.Fill(dtMainSQLData);
        DataColumnCollection dcCollection = dtMainSQLData.Columns;

        // Export Data into EXCEL Sheet
        Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
        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("C:/Users/Administrator.CAMO/Downloads/FtpFilesStorage/Sheet1.xlsx");
        ExcelApp.ActiveWorkbook.Saved = true;
        ExcelApp.Quit();

        Console.WriteLine(".xlsx file Exported succssessfully.");
}

And I am using this method in Main() like this:

System.Data.DataTable dtMainSQLData = new System.Data.DataTable();
ExportToExcel(dtMainSQLData);

This works fine but the first row is not copied or exported from the original table in SQL Server.

È stato utile?

Soluzione

You're experiencing one-off errors because of the fact that ExcelApp.Cells isn't zero based and you have a header row.

To combat this you can use foreach and separate out your header export to a separate block. It does take a little bit more code but I find it easier to deal with and less error prone

int i = 1;

int j = 1;   
//header row
foreach(DataColumn col in dtMainSQLData.Columns)
{  
  ExcelApp.Cells[i, j]  = col.ColumnName;
  j++;
}

i++;
//data rows
foreach(DataRow row  in dtMainSQLData.Rows)
{
    for (int k = 1; k < dtMainSQLData.Columns.Count + 1; k++)
    {
         ExcelApp.Cells[i, k]  = row[k-1].ToString();
    }
    i++;
}

Altri suggerimenti

Your code is fine, except you just need to remove your "else" block and increment the starting row of your Excel spreadsheet:

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();

        ExcelApp.Cells[i + 1, j] = dtMainSQLData.Rows[i - 1][j - 1].ToString();
    }
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top