zoukankan      html  css  js  c++  java
  • NPOI 1.2.4实现服务器无OFFICE组件导出EXCEL

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using NPOI.HSSF.UserModel;
    using NPOI.HPSF;
    using NPOI.POIFS.FileSystem;
    using NPOI.SS.Util;
    using NPOI.SS.UserModel;
    using System.Data;
    namespace WebApplication1
    {
    /// <summary>
    /// Summary description for DownloadExcel
    /// </summary>
    public class DownloadExcel : IHttpHandler
    {

    public void ProcessRequest(HttpContext context)
    {
    context.Response.ContentType = "application/x-excel";
    string sFileType = context.Request.QueryString["FileType"];

    switch (sFileType)
    {
    case "1": //Product Development Time Report
    SaveProductDeveTime(context);
    break;
    case "2": //Project Billable Report

    break;
    case "3": //Project Development Time Report

    break;
    case "4": //Staff Claim Control Report

    break;
    }

    context.Response.Write("Hello World");
    }

    public bool IsReusable
    {
    get
    {
    return false;
    }
    }


    private void SaveProductDeveTime(HttpContext context)
    {

    int rowIndex = 0;
    string filename = HttpUtility.UrlEncode("Product_Development_Time_Report.xls");//文件名进行url编码,防止乱码
    context.Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
    HSSFWorkbook workbook = new HSSFWorkbook();
    DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
    dsi.Company = "NPOI Team";
    workbook.DocumentSummaryInformation = dsi;
    SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
    si.Subject = "NPOI SDK Example";
    workbook.SummaryInformation = si;
    ISheet sheet = workbook.CreateSheet("Product DevelopmentTime Report");

    //设置报表的主标题信息
    IRow rowTitle = sheet.CreateRow(rowIndex);
    rowTitle.HeightInPoints = 20;

    ICell cellTitle = rowTitle.CreateCell(0);
    //set the title of the sheet
    cellTitle.SetCellValue("Product Development Time Report");
    ICellStyle styleTitle = workbook.CreateCellStyle();
    styleTitle.Alignment = HorizontalAlignment.CENTER;
    //create a font style
    IFont font = workbook.CreateFont();
    font.Boldweight = (short)FontBoldWeight.BOLD;//加粗
    font.FontHeightInPoints = 14;
    styleTitle.SetFont(font);
    cellTitle.CellStyle = styleTitle;
    rowIndex++;


    //merged cells on single row
    //ATTENTION: don't use Region class, which is obsolete
    sheet.AddMergedRegion(new CellRangeAddress(0, 0, 0, 2));


    //设置报表副标题
    IRow rowSubTitle = sheet.CreateRow(rowIndex);
    rowSubTitle.HeightInPoints = 18;
    ICell cellSubTitle = rowSubTitle.CreateCell(0);
    //set the title of the sheet
    cellSubTitle.SetCellValue("Period:25/02/2012-26/03/2012"); //区间页面传值
    ICellStyle styleSubTitle = workbook.CreateCellStyle();
    styleSubTitle.Alignment = HorizontalAlignment.CENTER;
    //create a font style
    IFont fontSubTitle = workbook.CreateFont();
    fontSubTitle.Boldweight = (short)FontBoldWeight.BOLD;//加粗
    fontSubTitle.FontHeightInPoints = 12;
    styleSubTitle.SetFont(fontSubTitle);
    cellSubTitle.CellStyle = styleSubTitle;
    sheet.AddMergedRegion(new CellRangeAddress(1, 1, 0, 2));
    rowIndex++;

    IRow rowSubTitle2 = sheet.CreateRow(rowIndex);
    rowSubTitle2.HeightInPoints = 18;
    ICell cellSubTitle2 = rowSubTitle2.CreateCell(0);
    //set the title of the sheet
    cellSubTitle2.SetCellValue("Time360"); //区间页面传值
    ICellStyle styleSubTitle2 = workbook.CreateCellStyle();
    styleSubTitle2.Alignment = HorizontalAlignment.CENTER;
    //create a font style
    IFont fontSubTitle1 = workbook.CreateFont();
    fontSubTitle1.Boldweight = (short)FontBoldWeight.BOLD;//加粗
    fontSubTitle1.FontHeightInPoints = 12;
    styleSubTitle2.SetFont(fontSubTitle1);
    cellSubTitle2.CellStyle = styleSubTitle2;
    sheet.AddMergedRegion(new CellRangeAddress(2, 2, 0, 2));
    rowIndex++;


    ICellStyle StyleCell = workbook.CreateCellStyle();//创建单元格的样式
    IFont FontCell = workbook.CreateFont();//创建字体样式
    FontCell.Boldweight = (short)FontBoldWeight.BOLD;//加粗
    FontCell.FontHeightInPoints = 12;
    FontCell.FontName = "Arial";

    StyleCell.SetFont(FontCell);
    StyleCell.Alignment = NPOI.SS.UserModel.HorizontalAlignment.CENTER;//横样式
    StyleCell.VerticalAlignment = VerticalAlignment.CENTER;//垂直样式
    StyleCell.BorderBottom = CellBorderType.THIN;
    StyleCell.BorderTop = CellBorderType.THIN;

    IRow rowHeaderTitle = sheet.CreateRow(rowIndex);
    string[] HeadcolumnsStr = new string[] { "Activeity Code ", "This Peroid(Manhours)", "To-Date(Manhours)" };
    for (int i = 0; i < HeadcolumnsStr.Length; i++)
    {
    ICell celltemp = rowHeaderTitle.CreateCell(i);

    celltemp.SetCellValue(HeadcolumnsStr[i]);

    celltemp.CellStyle = StyleCell;

    }
    rowIndex++;

    #region 数据
    StyleCell = workbook.CreateCellStyle();//创建单元格的样式
    FontCell = workbook.CreateFont();//创建字体样式
    FontCell.Boldweight = (short)FontBoldWeight.NORMAL;//普通
    FontCell.FontHeightInPoints = 12;

    FontCell.FontName = "Arial";

    StyleCell.SetFont(FontCell);
    StyleCell.Alignment = NPOI.SS.UserModel.HorizontalAlignment.LEFT;//横样式
    StyleCell.VerticalAlignment = VerticalAlignment.CENTER;//垂直样式
    StyleCell.BorderBottom = CellBorderType.THIN;
    StyleCell.BorderTop = CellBorderType.THIN;
    DataTable sourceTable_TestRecord = new DataTable();


    for (int i = 0; i < sourceTable_TestRecord.Rows.Count; i++)
    {
    IRow rowData = sheet.CreateRow(rowIndex);
    for (int j = 0; j < HeadcolumnsStr.Length; j++)
    {
    ICell cellValue = rowData.CreateCell(j);


    cellValue.SetCellValue(sourceTable_TestRecord.Rows[i][j].ToString());
    cellValue.CellStyle = StyleCell;
    }
    rowIndex++;
    }

    #endregion

    #region 统计

    StyleCell = workbook.CreateCellStyle();//创建单元格的样式
    FontCell = workbook.CreateFont();//创建字体样式
    FontCell.Boldweight = (short)FontBoldWeight.BOLD;//加粗
    FontCell.Color = NPOI.HSSF.Util.HSSFColor.RED.index;
    FontCell.FontHeightInPoints = 12;
    FontCell.FontName = "Arial";

    StyleCell.SetFont(FontCell);
    StyleCell.Alignment = NPOI.SS.UserModel.HorizontalAlignment.RIGHT;//横样式
    StyleCell.VerticalAlignment = VerticalAlignment.CENTER;//垂直样式

    IRow rowTotal = sheet.CreateRow(rowIndex);
    ICell cellTotalName0 = rowTotal.CreateCell(0);
    cellTotalName0.SetCellValue("Total Hours");
    cellTotalName0.CellStyle = StyleCell;


    StyleCell = workbook.CreateCellStyle();//创建单元格的样式
    FontCell = workbook.CreateFont();//创建字体样式
    FontCell.Boldweight = (short)FontBoldWeight.BOLD;//加粗
    FontCell.Color = NPOI.HSSF.Util.HSSFColor.RED.COLOR_NORMAL;
    FontCell.FontHeightInPoints = 12;
    FontCell.FontName = "Arial";

    StyleCell.SetFont(FontCell);
    StyleCell.Alignment = NPOI.SS.UserModel.HorizontalAlignment.RIGHT;//横样式
    StyleCell.VerticalAlignment = VerticalAlignment.CENTER;//垂直样式

    ICell cellTotalName1 = rowTotal.CreateCell(1);
    cellTotalName1.SetCellValue("200");
    cellTotalName1.CellStyle = StyleCell;

    #endregion


    #region 设置列宽
    sheet.SetColumnWidth(0, 12000);
    sheet.SetColumnWidth(1, 12000);
    sheet.SetColumnWidth(2, 12000);
    #endregion



    //////merged cells on mutiple rows
    //CellRangeAddress region = new CellRangeAddress(3, 4, 3, 4);

    //sheet.AddMergedRegion(region);


    ////set enclosed border for the merged region
    //((HSSFSheet)sheet).SetEnclosedBorderOfRegion(region, CellBorderType.DOTTED, NPOI.HSSF.Util.HSSFColor.RED.index);


    workbook.Write(context.Response.OutputStream);

    }

    }
    }
  • 相关阅读:
    准备 FRM 考试——方法、工具与教训
    930. 和相同的二元子数组 前缀和
    1906. 查询差绝对值的最小值 前缀和
    剑指 Offer 37. 序列化二叉树 二叉树 字符串
    815. 公交路线 BFS
    518. 零钱兑换 II dp 完全背包
    1049. 最后一块石头的重量 II dp
    5779. 装包裹的最小浪费空间 二分
    5778. 使二进制字符串字符交替的最少反转次数 字符串 滑动窗口
    474. 一和零 dp
  • 原文地址:https://www.cnblogs.com/Loofah/p/NPOI.html
Copyright © 2011-2022 走看看