页面table?我这里有一段程序,你看看是否有帮助!
其中dt为DataTable //存储表单数据
Excel.Application excel=null;
Excel.Workbook workbook =null;
try
{
excel= new Excel.Application();
int rowIndex=1;
int colIndex=0;
workbook=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;
foreach(DataColumn col in dt.Columns)
{
colIndex++;
excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
}
}
workbook.Saved=true;
excel.Save("c:\\aaa.xls");
MessageBox.Show("文件保存成功!","提示!");
}
catch (System.Security.SecurityException ex)
{
MessageBox.Show("拒绝授予保存该文件的权限,未保存此文件!","提示!");
}
catch (Exception ex)
{
MessageBox.Show("文件保存失败!","提示!");
}
finally
{
excel.Quit();
excel=null;
GC.Collect();
}
其中dt为DataTable //存储表单数据
Excel.Application excel=null;
Excel.Workbook workbook =null;
try
{
excel= new Excel.Application();
int rowIndex=1;
int colIndex=0;
workbook=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;
foreach(DataColumn col in dt.Columns)
{
colIndex++;
excel.Cells[rowIndex,colIndex]=row[col.ColumnName].ToString();
}
}
workbook.Saved=true;
excel.Save("c:\\aaa.xls");
MessageBox.Show("文件保存成功!","提示!");
}
catch (System.Security.SecurityException ex)
{
MessageBox.Show("拒绝授予保存该文件的权限,未保存此文件!","提示!");
}
catch (Exception ex)
{
MessageBox.Show("文件保存失败!","提示!");
}
finally
{
excel.Quit();
excel=null;
GC.Collect();
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货