如何将数据表中的数据导出到具有正确输出的 excel 文件中。i 表示 excel 记录的第一行应该是标题,显示数据应该显示所有值,而不是四舍五入。请帮助 ** ldt_Temp (datatable),as_OutputDir (exported dir)
object misValue = System.Reflection.Missing.Value;
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
app.Visible = false;
Microsoft.Office.Interop.Excel.Workbook wb = app.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)wb.ActiveSheet;
// Headers.
for (int i = 0; i < ldt_Temp.Columns.Count; i++)
{
ws.Cells[1, i + 1] = ldt_Temp.Columns[i].ColumnName;
}
// Content.
for (int i = 0; i < ldt_Temp.Rows.Count; i++)
{
for (int j = 0; j < ldt_Temp.Columns.Count; j++)
{
ws.Cells[i + 2, j + 1] = ldt_Temp.Rows[i][j].ToString();
}
}
ws.Name = ldt_Temp.TableName;
wb.SaveAs(as_OutputDir, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
wb.Close(true, misValue, misValue);
app.Quit();