• NPOI excel导出快速构建


    直接上代码,这个是一个在webFrom中的例子,要请求的页面是ashx

     public void ExportVisaFeeAll(HttpContext context)
            {
                try
                {
                    string title ="签证费用表";
    
                    string[] idArray = context.Request["idStr"].ToString().Split(',');
                    DateTime startDate = DateTime.Parse(context.Request["startDate"]);
                    DateTime endDate = DateTime.Parse(context.Request["endDate"]).AddDays(1);
    
                    string unit = context.Request.QueryString["unit"];
                    string groupname = context.Request.QueryString["groupname"];
                    string countryname = context.Request.QueryString["countryname"];
                    string Member = context.Request.QueryString["Member"];
                    string LetterNumbers = context.Request.QueryString["LetterNumbers"];
                    List<VisaFeeMain> visaFees = new OilDigital.CGGL.Web.VisaApply.VisaApplyProcess().GetVisaFeeByQueryString(startDate, endDate, unit, groupname, countryname, Member, LetterNumbers);
                    for (int i = visaFees.Count-1; i >=0; i--)
                    {
                        bool flag = true;
                        for (int j  = 0; j < idArray.Length; j++)
                        {
                            if (visaFees[i].Id.ToString() == idArray[j])
                            {
                                flag = false;
                                break;
                            }
                        }
                        if (flag)
                        {
                            visaFees.RemoveAt(i);
                        }
                    }
                    HSSFWorkbook wb = new HSSFWorkbook();//创建工作薄
                    HSSFSheet sheet = (HSSFSheet)wb.CreateSheet(title); //创建工作表
                    HSSFRow row_Title = (HSSFRow)sheet.CreateRow(0); //创建列头行
                    row_Title.HeightInPoints = 19.5F; //设置列头行高
                    string[] titleStr = { "批件文号", "申办国家(地区)", "总金额", "人员名单", "总人数", "备注", "创建时间", "创建人"};
                    //设置列宽
                    for (int i = 0; i < titleStr.Length; i++)
                    {
                           sheet.SetColumnWidth(i, 20 * 256);
    
                    }
                    //设置列样式
                    HSSFCellStyle cs_Title = (HSSFCellStyle)wb.CreateCellStyle(); //创建列头样式
                    cs_Title.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; //水平居中
                    cs_Title.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; //垂直居中
                    HSSFFont cs_Title_Font = (HSSFFont)wb.CreateFont(); //创建字体
                    cs_Title_Font.FontHeightInPoints = 12; //字体大小
                    cs_Title_Font.Boldweight = (short)FontBoldWeight.Bold;
                    cs_Title.SetFont(cs_Title_Font); //将字体绑定到样式
                    //填充列头内容
                    for (int i = 0; i < titleStr.Length; i++)
                    {
                        HSSFCell cell_Title = (HSSFCell)row_Title.CreateCell(i); //创建单元格
                        cell_Title.CellStyle = cs_Title; //将样式绑定到单元格
                        cell_Title.SetCellValue(titleStr[i]);
                    }
                    //设置单元格样式
                    HSSFCellStyle cs_Content = (HSSFCellStyle)wb.CreateCellStyle(); //创建列头样式
                    cs_Content.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; //水平居中
                    cs_Content.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; //垂直居中
                    //填充单元格
                    int r = 0;
                    for (r = 0; r < visaFees.Count; r++)
                    {
                        int i = r;
                        HSSFRow row = (HSSFRow)sheet.CreateRow(i + 1);
                        row.HeightInPoints = 16;
                        string[] arr = { visaFees[i].LetterNumbers.ToString(), visaFees[i].EmbassyNames, visaFees[i].Fee, visaFees[i].Names, visaFees[i].TotalAmount.ToString(), visaFees[i].Memo, visaFees[i].CreatedTime.ToString("yyyy-MM-dd"), visaFees[i].CreatedBy };
                        for (int j = 0; j < arr.Length; j++)
                        {
                            HSSFCell cell = (HSSFCell)row.CreateCell(j); //创建单元格
                            cell.CellStyle = cs_Content;
                                cell.SetCellValue(arr[j]);
    
                        }
                    }
                  
    
                    HttpResponse httpResponse = HttpContext.Current.Response;
                    httpResponse.Clear();
                    httpResponse.Buffer = true;
                    //httpResponse.Charset = Encoding.UTF8.BodyName;
                    httpResponse.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("签证费用.xls"));
                    httpResponse.ContentEncoding = System.Text.Encoding.UTF8;
                    httpResponse.ContentType = "application/vnd.ms-excel; charset=UTF-8";
                    wb.Write(httpResponse.OutputStream);
                    httpResponse.End();
                }
                catch (Exception ex)
                {
    
                    throw new Exception(ex.Message);
                }
            }
  • 相关阅读:
    Permission denied (publickey). fatal: Could not read from remote repository.
    jQuery OCUpload一键上传文件
    org.apache.subversion.javahl.ClientException: Working copy is not up-to-date
    测开之路一百三十五:实现登录身份验证功能
    测开之路一百三十四:实现指定查找功能
    测开之路一百三十三:实现sql函数封装
    测开之路一百三十二:实现修改功能
    测开之路一百三十一:实现删除功能
    测开之路一百三十:实现前端到数据库交互(增和查)
    测开之路一百二十九:jinja2模板语法
  • 原文地址:https://www.cnblogs.com/zhengwei-cq/p/10219320.html
Copyright © 2020-2023  润新知