110,534
社区成员
发帖
与我相关
我的任务
分享
using Excel;
string FPath=@System.Windows.Forms.Application.StartupPath+"\\AAA.xls";
Excel.ApplicationClass ex=new Excel.ApplicationClass();
ex.Application.Workbooks.Add(FPath);
ex.Cells[1,1]="赋值";
ex.Visible=true;
protected void AddExcel(DataSet ds)
{
DataTable dt = ds.Tables[0];
string fileName = Guid.NewGuid() + ".xls";
Excel.Application excel = new Excel.ApplicationClass();
int rowIndex = 1;
int colIndex = 0;
excel.Application.Workbooks.Add(true);
foreach (DataColumn col in dt.Columns)
{
colIndex++;
excel.Cells[1, colIndex] = col.ColumnName;
}
foreach (DataRow row in dt.Rows)
{
rowIndex++;
colIndex = 0;
for (colIndex = 0; colIndex < dt.Columns.Count; colIndex++)
{
excel.Cells[rowIndex, colIndex + 1] = row[colIndex].ToString();
}
}
excel.Visible = false;
excel.ActiveWorkbook.SaveAs(fileName, Excel.XlFileFormat.xlExcel9795, null, null, false, false, Excel.XlSaveAsAccessMode.xlNoChange, null, null, null, null, null);
//excel.Save(fileName);
excel.Quit();
excel = null;
GC.Collect();//垃圾回收
}