日期:2014-05-17 浏览次数:20483 次
public static void DataTabletoExcel(DataTable tmpDataTable, string strFileName)
{
if (tmpDataTable == null)
return;
int rowNum = tmpDataTable.Rows.Count;
int columnNum = tmpDataTable.Columns.Count;
Microsoft.Office.Interop.Excel.ApplicationClass MyExcel = new Microsoft.Office.Interop.Excel.ApplicationClass();
MyExcel.Visible = false;//excel是否可见
MyExcel.DisplayAlerts = false;//屏蔽一些弹出窗口
Microsoft.Office.Interop.Excel.Workbooks MyWorkBooks = MyExcel.Workbooks;
Microsoft.Office.Interop.Excel.Workbook MyWorkBook = MyWorkBooks.Add(System.Type.Missing);
Microsoft.Office.Interop.Excel.Worksheet MyWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)MyWorkBook.Worksheets[1];
//ar xlApp = new Application();
//xlApp.DefaultFilePath = "";
//xlApp.DisplayAlerts = true;
//xlApp.SheetsInNewWorkbook = 1;
//Workbook xlBook = xlApp.Workbooks.Add(true);
//MyWorkSheet.Cells[1, 1] = projectName + " 进度计划";
//导入列名
for (int i = 0; i < columnNum; i++)
{
MyWorkSheet.Cells[1, i + 1] = tmpDataTable.Columns[i].ColumnName;
}
//将DataTable中的数据导入Excel中
for (int i = 0; i < rowNum; i++)
{
for (int j = 0; j < columnNum; j++)
{
MyWorkSheet.Cells[i + 2, j + 1] = tmpDataTable.Rows[i][j].ToStri