将table信息导出到excel
2012-04-10 11:06 愿心平静面对 阅读(290) 评论(0) 编辑 收藏 举报这两天需要做一个导出到excel的功能,因为老是出现浏览器不兼容的问题,不过最后找到了个,自己稍微把它封装了下,跟大家分享下。
public void Excelxls(HtmlTable tabExcel)
{
try
{
//获取到页面的table
XlsDocument xls = new XlsDocument();
xls.FileName = 2012 + "_年度报表.xls";
string sheetName = "test";
int rowMin = 1;
int rowCount = tabExcel.Rows.Count; //获取到共多少行
int colMin = 1;
int colCount = tabExcel.Rows[0].Cells.Count; //获取到共多少列
Worksheet sheet = xls.Workbook.Worksheets.AddNamed(sheetName);
HtmlTableRowCollection tabRow = tabExcel.Rows;
Cells cells = sheet.Cells;
for (int r = 0; r < rowCount; r++)
{
if (r == 0)
{
for (int i = 0; i < colCount; i++)
{
//在一行内创建colCount个单元格
cells.Add(1, colMin + i, tabRow[0].Cells[i].InnerText).Font.Bold = true;
}
}
else
{
for (int c = 0; c < colCount; c++)
{
Cell cell = cells.Add(r + rowMin, c + colMin, tabRow[r].Cells[c].InnerText);//从第二行的第一个单元格开始填充
}
}
}
xls.Send();
}
catch (Exception ex)
{
this.ClientScript.RegisterStartupScript(this.GetType(), "alert_msg", "<script>alert(\"" + ex.Message + "\")</script>");
}
}
写完这个方法,然后页面继承它,调用这个方法就OK了,还有就是这个dll文件哦
这个是下载地址:http://iiidown.com/source/32001487