日期:2014-05-17 浏览次数:20578 次
string excelName ="\\"+ DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls";
string path = Server.MapPath("upfiles") + excelName;
Excel.Application excel = new Excel.Application(); //Execl的操作类
//读取保存目标的对象
Excel.Workbook bookDest = (Excel.WorkbookClass)excel.Workbooks.Add(Missing.Value);
Excel.Worksheet sheetDest = bookDest.Worksheets.Add(Missing.Value, Missing.Value, Missing.Value, Missing.Value) as Excel.Worksheet;//给工作薄添加一个Sheet
sheetDest.Rows.RowHeight = 20;
sheetDest.Name = strSheetName;
int rowIndex = 1;
int colIndex = 0;
excel.Application.Workbooks.Add(true);
foreach (DataColumn col in dt.Columns)
{
colIndex++;
sheetDest.Cells[1, colIndex] = col.ColumnName;
}
//导入数据行
foreach (DataRow row in dt.Rows)
{
rowIndex++;
colIndex = 0;
foreach (DataColumn col in dt.Columns)
{
colIndex++;
sheetDest.Cells[rowIndex, colIndex] = row[col.ColumnName].ToString();
}
}
bookDest.Saved = true;
bookDest.SaveCopyAs(path);
excel.Quit();
excel = null;
GC.Collect();