• 使用NPOI写入Excel数据(ASP.NET)


    第一次做这个写入数据的功能,研究了下npoi的类

           IWorkbook wb = new HSSFWorkbook();
    
            //创建表  
            ISheet sh = wb.CreateSheet("XXX");
            //设置单元的宽度  
            sh.SetColumnWidth(0, 20 * 256);
    
    
            #region 合并单元格
    
           
            IRow row0 = sh.CreateRow(0);
            row0.Height = 20 * 20;
            ICell icell1top0 = row0.CreateCell(0);
            icell1top0.CellStyle = Getcellstyle(wb, stylexls.头);
            icell1top0.SetCellValue(getTitle());
    
            #endregion
    
            #region 创建问题题目
            IRow row1 = sh.CreateRow(1);
            // row1.Height = 20 * 20;
    
            ICell icell = null;
    
            ICell icell1top = row1.CreateCell(0);
            icell1top.CellStyle = Getcellstyle(wb, stylexls.头);
            icell1top.SetCellValue("姓名/问卷题目");
    
            string sqlAll = "select * from dbo.CheckMgr where UserID=" + rqid + " order by T_Sort asc";
            DataTable dtAll = SystemDAL.SQLHelper.GetTable(sqlAll);
            if (dtAll != null && dtAll.Rows.Count > 0)
            {
                for (int j = 0; j < dtAll.Rows.Count; j++)
                {
                    sh.SetColumnWidth(j + 1, 40 * 256);
                    //合并单元格//表头
     //CellRangeAddress()该方法的参数次序是:开始行号,结束行号,开始列号,结束列号。 sh.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, dtAll.Rows.Count)); string title = dtAll.Rows[j]["T_title"].ToString(); icell = row1.CreateCell(j + 1); icell.SetCellValue(title); } } #endregion #region 创建对应题目的答案内容 DataTable dtUser = getUserInfo(); IRow row11 = null; for (int a = 0; a < dtUser.Rows.Count; a++)//学生 { row11 = sh.CreateRow(a + 2); string user = dtUser.Rows[a]["realname"].ToString(); string id = dtUser.Rows[a]["id"].ToString(); for (int b = 0; b < dtAll.Rows.Count; b++)//题目 { //每一行的第一列//用户姓名 HSSFCell cell11 = row11.CreateCell(0) as HSSFCell; cell11.SetCellValue(user); DataTable dtInfo = getAnwer(id); if (dtInfo != null && dtInfo.Rows.Count > 0) { for (int i = 0; i < dtInfo.Rows.Count; i++) { string key = dtInfo.Rows[b]["Ckey"].ToString(); string value = dtInfo.Rows[b]["Cvalue"].ToString(); string title = getMgrTitle(key, value); HSSFCell cell1 = row11.CreateCell(b + 1) as HSSFCell; cell1.SetCellValue(title); } } } } #endregion #region 下载Excel Random r = new Random(); string _fileName = string.Format("{0:yyyyMMddHHmmssfff}-{1}", DateTime.Now, r.Next(1, 9999)) + ".xls"; using (MemoryStream ms = new MemoryStream()) { wb.Write(ms); Response.Clear(); Response.ClearHeaders(); Response.Buffer = false; Response.ContentType = "application/octet-stream"; Response.AppendHeader("Content-Disposition", "attachement;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.ASCII)); Response.AppendHeader("Content-Length", ms.Length.ToString()); Response.BinaryWrite(ms.GetBuffer()); Response.Flush(); Response.End(); } #endregion


    效果:

  • 相关阅读:
    七月二十二日学习报告
    七月二十一学习报告
    七月二十学习报告
    补 七月十九学习报告
    七月十八学习报告
    iOS 逆向工程(三)之Xcode检测是否越狱成功
    iOS 逆向工程(二)之越狱手机安装必备软件
    iOS逆向工程(一)之手机越狱
    html练习 简单html页面
    集合对象序列化 反序列 IO 1将文件中的所有信息,通过合适的IO流读取出来,封装成Person对象,使用集合进行存储 2将集合对象序列化到另外一个文件persons.txt中 3从persons.txt反序列化其中的集合,并遍历集合内容
  • 原文地址:https://www.cnblogs.com/vanteking/p/4409975.html
Copyright © 2020-2023  润新知