zoukankan      html  css  js  c++  java
  • 前台table里的值通过npoi导出excle Jimmy

    前台获取table里的所有值:                                                                        

     var tableInfo = "";
             var tableObj = document.getElementById("dg1");
             for (var i = 0; i < tableObj.rows.length; i++) {    //遍历Table的所有Row
                 for (var j = 0; j < tableObj.rows[i].cells.length; j++) {   //遍历Row中的每一列
    
                     tableInfo += tableObj.rows[i].cells[j].innerHTML;
                     //获取Table中单元格的内容
                     if (j != tableObj.rows[i].cells.length - 1) {
                         tableInfo += ",";
                     }
    
                 }
                 if (i != tableObj.rows.length - 1) {
                     tableInfo += "|";
                 }
             }

    这里数据组合成一个字符串  tr用'|'分割  td用','分割    如: 姓名,性别,年龄|qq,男,26|qqq,女,22

    后台将数据导入Excle并导出:                                                                      

    这里我引用npoi的1.2正式版 资料学习:http://www.cnblogs.com/tonyqus/archive/2009/04/12/1434209.html  最新是2.0的  不过实现简单功能足够了

    这里首先添加引用dll文件

    using NPOI.HSSF.UserModel;
    using NPOI.HPSF;
    using NPOI.POIFS.FileSystem;
    using NPOI.HSSF.Util;
    

     生成说明代码:

    View Code
     protected void Button1_Click(object sender, EventArgs e)
            {
                try
                {
                    string perinfo = this.perinfo.Value.ToString();//接受前台table 数值字符串
                    int pernum = Convert.ToInt32(this.pernum.Value);//接受前台值  获得行数 也可通过perinfo获得
                    string[] perinfors = perinfo.Split('|');
    
                    InitializeWorkbook();//步骤
    
                    HSSFSheet sheet1 = hssfworkbook.CreateSheet("Sheet1");//创建一个单元  excle默认是三个
    
                    sheet1.AddMergedRegion(new Region(0, 0, 0, 17));//设置合并单元格   合并第一行 1-18列 空的 可以加标题
                    //设置字体样式
                    //HSSFCellStyle style = hssfworkbook.CreateCellStyle();
                    //style.Alignment = HSSFCellStyle.ALIGN_CENTER;
                    //HSSFFont font = hssfworkbook.CreateFont();
                    //font.FontHeight = 20 * 20;
                    //style.SetFont(font);
                    for (int i = 1; i < (pernum + 1); i++)//将table的tr 分别加入到单元格中 这里没有详细判断GetRow 是否为空  不判断容易出错
                    {
                        string[] perinfo2 = perinfors[i - 1].Split(',');
                        HSSFRow row = sheet1.CreateRow(i);
                        for (int j = 0; j < 18; j++)
                        {
                            row.CreateCell(j).SetCellValue(perinfo2[j]);
    
                        }
                    }
                    //if (sheet1.GetRow(0) != null)
                    //{
                    //    HSSFCell cell = sheet1.GetRow(0).GetCell(0);
                    //    cell.CellStyle = style;
                    //}
    
                    WriteToFile();//步骤3
                    wr(ppath);//导出   这里只提供一种下载方式
                }
                catch (Exception ex)
                {
                    Response.Write("<script>alert('导出失败!');</script>");
                }
            }
            static HSSFWorkbook hssfworkbook;
            public string ppath;
            public void WriteToFile()
            {
                //Write the stream data of workbook to the root directory
                string year = DateTime.Now.Year.ToString();
                ppath = HttpContext.Current.Server.MapPath("downxls/" + year + "年招聘新员工所需信息.xls");
                FileStream file = new FileStream(ppath, FileMode.Create);
                hssfworkbook.Write(file);
                file.Close();
               
            }
            public void InitializeWorkbook()
            {
                hssfworkbook = new HSSFWorkbook();
                ////create a entry of DocumentSummaryInformation
                DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
                dsi.Company = "company";
                hssfworkbook.DocumentSummaryInformation = dsi;
                ////create a entry of SummaryInformation
                SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
                si.Subject = "xxx";
                hssfworkbook.SummaryInformation = si;
            }
            public void wr(string ppath)
            {
                if (File.Exists(ppath))
                {
                    Response.ClearHeaders();
                    Response.Clear();
                    Response.Expires = 0;
                    Response.Buffer = true;
                    Response.AddHeader("Accept-Language", "zh-tw");
                    string name = System.IO.Path.GetFileName(ppath);
                    System.IO.FileStream files = new FileStream(ppath, FileMode.Open, FileAccess.Read, FileShare.Read);
                    byte[] byteFile = null;
                    if (files.Length == 0)
                    {
                        byteFile = new byte[1];
                    }
                    else
                    {
                        byteFile = new byte[files.Length];
                    }
                    files.Read(byteFile, 0, (int)byteFile.Length);
                    files.Close();
    
                    Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8));
                    Response.ContentType = "application/octet-stream;charset=gbk";
                    Response.BinaryWrite(byteFile);
                    Response.End();
    
    
                }
            }

    生成excle效果图:

    简单例子 仅供参考

  • 相关阅读:
    百鸡问题-小记
    vs2005下opengl(glut)的配置记录
    GSL+DevC++使用
    VS2005下使用GSL-1.15小结
    GSL--GNU Scientific Library 小记
    Window下设置Octave
    0欧姆电阻和磁珠的作用
    关于接地:数字地、模拟地、信号地、交流地、直流地、屏蔽地、浮地,特别经典哦
    altium designer设置不会产生.pcbpreview、.schpreview等的垃圾文件
    【剑指Offer面试题】 九度OJ1516:调整数组顺序使奇数位于偶数前面
  • 原文地址:https://www.cnblogs.com/DemoLee/p/2830943.html
Copyright © 2011-2022 走看看