• 将table信息导出到excel


    这两天需要做一个导出到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

  • 相关阅读:
    Win10 主题 美化 动漫
    Win10 主题 美化 动漫
    span 居中
    This version of MySQL doesn't yet support 'LIMIT & IN/ALL/ANY/SOME subquery
    10 Future Web Trends 十大未来互联网趋势
    10 Future Web Trends 十大未来互联网趋势
    使用pycharm进行简单的数据库管理
    使用pycharm进行简单的数据库管理
    Python开发利器PyCharm 2.7附注册码
    Python开发利器PyCharm 2.7附注册码
  • 原文地址:https://www.cnblogs.com/YuanShuai/p/2440170.html
Copyright © 2020-2023  润新知