zoukankan      html  css  js  c++  java
  • 将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

  • 相关阅读:
    js去前后空格
    IE7以上支持Fiddler 监听本地
    IE8 scriptX print 无法使用的bug
    那些相见恨晚的 JavaScript 技巧
    oracle sql
    JS人民币金额转大写程序
    div自适应高度
    .NET Remoting 使用最佳实践,(部分翻译)
    对DataTable 进行Distinct操作
    用财富的眼光看知识管理
  • 原文地址:https://www.cnblogs.com/YuanShuai/p/2440170.html
Copyright © 2011-2022 走看看