예제 #1
0
 public static void SaveSheet(NPOIBaseHandler handler, ref ISheet sheet, int rowStart = 0, int colStart = 0, bool rownum = true)
 {
     if (HttpContext.Current != null && HttpContext.Current.Response != null)
     {
         int rowEnd = handler.Write(sheet, rowStart, colStart, rownum);
         handler.WriteColumnSum(sheet, rowStart, rowEnd, colStart, rownum);
     }
 }
예제 #2
0
        //public static ISheet CreateSheet(string sheetname = null)
        //{
        //    HSSFWorkbook hssfworkbook = new HSSFWorkbook();
        //    ISheet sheet = hssfworkbook.CreateSheet(sheetname != null ? sheetname : "Sheet1");
        //    return sheet;
        //}

        //public static ISheet Write(ISheet sheet,NPOIBaseHandler hander, string title, int rowStart = 0, int colStart = 0, bool rownum = true, List<String> headers = null)
        //{
        //    IRow row = null;
        //    int rOffset = 0;
        //    if(headers == null){
        //        headers = hander.GetDefaultFields();
        //    }

        //    //Title
        //    if (false == string.IsNullOrEmpty(title))
        //    {
        //        SetRegionValue(title, sheet, rOffset++, 0, headers.Count);
        //    }
        //    //Header
        //    row = sheet.CreateRow(rOffset);
        //    ICellStyle style = sheet.Workbook.CreateCellStyle();
        //    style.Alignment = HorizontalAlignment.Center;
        //    style.VerticalAlignment = VerticalAlignment.Center;
        //    IFont font = sheet.Workbook.CreateFont();
        //    font.FontHeight = 18 * 18;
        //    font.Boldweight = 15 * 15;
        //    style.SetFont(font);
        //    style.FillBackgroundColor = HSSFColor.LIGHT_BLUE.index;

        //    SetHeaders(headers, row, colStart++, style);

        //    Create(sheet, colStart, 1, headers.Count, colStart);

        //    //Data
        //    hander.Write(sheet, rOffset, colStart, rownum);
        //    return sheet;
        //}

        //public static bool WriteToBrowser(ISheet sheet, string filename){
        //    if(HttpContext.Current != null && HttpContext.Current.Response != null){
        //        HttpResponse Response = HttpContext.Current.Response;
        //        Response.Clear();
        //        sheet.Workbook.Write(Response.OutputStream);
        //        Response.ContentType = "application/octet-stream";
        //        Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\";");
        //        return true;
        //    }
        //    return false;
        //}

        /// <summary>
        /// 将DataTable中的值按指定位置导出为Excel
        /// </summary>
        /// <param name="dt">DataTable数据集</param>
        /// <param name="templetePath">模板路径</param>
        /// <param name="filename">输出文件名</param>
        /// <param name="rowStart">(可选)起始行</param>
        /// <param name="colStart">(可选)起始列</param>
        /// <param name="rownum">(可选)是否列号</param>
        public static void ExportAsExcel(NPOIBaseHandler handler, string templetePath, string filename, int rowStart = 0, int colStart = 0, bool rownum = true)
        {
            if (HttpContext.Current != null && HttpContext.Current.Response != null)
            {
                HttpResponse Response = HttpContext.Current.Response;

                using (FileStream fs = new FileStream(templetePath, FileMode.Open, FileAccess.Read))
                {
                    HSSFWorkbook hssfworkbook = new HSSFWorkbook(fs);
                    ISheet       sheet        = hssfworkbook.GetSheetAt(0);

                    int rowEnd = handler.Write(sheet, rowStart, colStart, rownum);
                    handler.WriteColumnSum(sheet, rowStart, rowEnd, colStart, rownum);

                    //发送到前台
                    Response.Clear();
                    hssfworkbook.Write(Response.OutputStream);
                    Response.ContentType     = "application/octet-stream";
                    Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
                    Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\";");
                    fs.Close();
                }
            }
        }