本文介绍了sql表中的第一行没有动态导出到excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将表'clientinfo'中所有4行的4列导出到Excel电子表格中。它可以离开表格的第一行。但是,数据表'dtMainSQLData'显示所有4行完整。电子表格只显示3行。

我的代码;



SqlDataAdapter da = new SqlDataAdapter(select clientname,clientaddress1,clientaddress2,clientaddress3 from clientinfo,sqlCon);

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

da.Fill(dtMainSQLData);

DataColumnCollection dcCollection = dtMainSQLData.Columns;

//将数据导出到EXCEL表中

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 \\RAJENDRAN \\Desktop\\ raj2.xls);

ExcelApp.ActiveWorkbook.Saved = true;

ExcelApp.Quit();

}

I want to export 4 columns of all 4 rows from table 'clientinfo' to an excel spreadsheet. It works leaving the first row of the table. However, the data table 'dtMainSQLData' shows all the 4 rows intact. The Spreadsheet shows only 3 rows.
My code;

SqlDataAdapter da = new SqlDataAdapter("select clientname,clientaddress1,clientaddress2,clientaddress3 from clientinfo", 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\\RAJENDRAN\\Desktop\\raj2.xls");
ExcelApp.ActiveWorkbook.Saved = true;
ExcelApp.Quit();
}

推荐答案

for(int i = 0 ...)
for(int j = 0 ...)


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





更改:

1-'else'已删除。

excel表格中从2开始的2行。



ps:使用调试器,最简单的方法。



changes:
1- 'else' removed.
2- rows started from 2 on excel sheet.

ps: use the debugger, the easiest way.


这篇关于sql表中的第一行没有动态导出到excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 22:36