这两天需要做一个导出到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
原文链接: https://www.cnblogs.com/YuanShuai/archive/2012/04/10/2440170.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/46952
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!