我正在尝试将 Datatable 存储在用 c#(SSIS ScriptTask)生成的 excel 中。
下面是我的代码:
OleDbDataAdapter A = new OleDbDataAdapter();
System.Data.DataTable dt = new System.Data.DataTable();
A.Fill(dt, Dts.Variables["User::ObjResultSet"].Value);
Excel.Application oXL = new Excel.ApplicationClass();
Excel.Workbooks oWBs = oXL.Workbooks;
Excel.Workbook oWB = null;
Excel.Worksheet oSheet;
oWB = oWBs.Count > 0 ? oWB = oWBs[0] : oWBs.Add(System.Reflection.Missing.Value);
oXL.DisplayAlerts = false;
oWB = oXL.Workbooks.Add(Missing.Value);
oSheet = (Excel.Worksheet)oWB.Worksheets[1];
int rowCount = 1;
foreach (DataRow dr in dt.Rows)
{
rowCount += 1;
for (int i = 1; i < dt.Columns.Count + 1; i++)
{
// Add the header time first only
if (rowCount == 2)
{
oSheet.Cells[1, i] = dt.Columns[i - 1].ColumnName;
}
oSheet.Cells[rowCount, i] = dr[i - 1].ToString();
}
}
oWB.SaveAs(Dts.Variables["User::ExcelPath"].Value, Excel.XlFileFormat.xlWorkbookNormal,
Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Excel.XlSaveAsAccessMode.xlExclusive,
Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value);
oWB.Close(false, Dts.Variables["User::ExcelPath"].Value, Missing.Value);
oWBs.Close();
oXL.Quit();
Dts.TaskResult = (int)ScriptResults.Success;
我面临的问题是,生成了excel。但是当我尝试生成.xls
时,它工作得很好。但是当我尝试生成.xlsx
.
谁能帮我?