1. 程式人生 > >.net webapi 後臺匯出excel 申請付款單例項

.net webapi 後臺匯出excel 申請付款單例項

        [HttpGet, AllowAnonymous]
        public void ExportSettlementPrint(string code)
        {
            FinSettlementModel settlementDetailModels = this._finSettlementService.GetFinSettlementByCode(code);//付款單

            #region 匯出excel 樣式設定
            HSSFWorkbook workbook = new HSSFWorkbook();//
建立空白工作簿 ISheet sheet = workbook.CreateSheet();//在工作簿中,建立空白工作表 //title 第0行 IRow row = sheet.CreateRow(0);//新建行 ICell cell = row.CreateCell(0);//新建列 cell.SetCellValue(code); ICellStyle stylecell0 = workbook.CreateCellStyle(); stylecell0.Alignment
= HorizontalAlignment.Left;//設定單元格樣式:水平對齊居中 stylecell0.VerticalAlignment = VerticalAlignment.Center; stylecell0.BorderBottom = BorderStyle.Thin; stylecell0.BorderLeft = BorderStyle.Thin; //stylecell0.BorderRight = BorderStyle.Thin; stylecell0.BorderTop = BorderStyle.Thin; IFont fontcell0
= workbook.CreateFont();//字型樣式 fontcell0.Boldweight = short.MaxValue;//字型加粗 fontcell0.FontHeightInPoints = 10;//字型大小 stylecell0.SetFont(fontcell0); cell.CellStyle = stylecell0; ICell cell1t = row.CreateCell(1); cell1t.SetCellValue("付款申請單"); ICellStyle style0 = workbook.CreateCellStyle(); style0.Alignment = HorizontalAlignment.Center;//設定單元格樣式:水平對齊居中 style0.VerticalAlignment = VerticalAlignment.Center; style0.BorderBottom = BorderStyle.Thin; // style.BorderLeft = BorderStyle.Thin; style0.BorderRight = BorderStyle.Thin; style0.BorderTop = BorderStyle.Thin; IFont font0 = workbook.CreateFont();//字型樣式 font0.Boldweight = short.MaxValue;//字型加粗 font0.FontHeightInPoints =10;//字型大小 style0.SetFont(font0); cell1t.CellStyle = style0; ICellStyle style = workbook.CreateCellStyle(); style.Alignment = HorizontalAlignment.Center;//設定單元格樣式:水平對齊居中 style.VerticalAlignment = VerticalAlignment.Center; style.BorderBottom = BorderStyle.Thin; style.BorderLeft = BorderStyle.Thin; style.BorderRight = BorderStyle.Thin; style.BorderTop = BorderStyle.Thin; IFont font = workbook.CreateFont();//字型樣式 font.Boldweight = short.MaxValue;//字型加粗 font.FontHeightInPoints = 10;//字型大小 style.SetFont(font); for (int p1 = 2; p1 < 8; p1++) { ICell rowJrowCell1 = row.CreateCell(p1); rowJrowCell1.SetCellValue(""); rowJrowCell1.CellStyle = style; } row.Height = 30 * 20; //統一樣式 ICellStyle styleSame = workbook.CreateCellStyle(); styleSame.BorderBottom = BorderStyle.Thin; styleSame.BorderLeft = BorderStyle.Thin; styleSame.BorderRight = BorderStyle.Thin; styleSame.BorderTop = BorderStyle.Thin; styleSame.Alignment = HorizontalAlignment.Center; styleSame.VerticalAlignment = VerticalAlignment.Center; IFont fontSame = workbook.CreateFont(); fontSame.FontHeightInPoints = 10; styleSame.SetFont(fontSame); //統一樣式 #region 第一行資料 //第一行 第一單元格 IRow row1 = sheet.CreateRow(1); ICell cell1 = row1.CreateCell(0); cell1.SetCellValue("日期:"); cell1.CellStyle = styleSame; // 2 ICell cell2 = row1.CreateCell(1); cell2.SetCellValue(settlementDetailModels.createdtime.ToShortDateString());//建立時間 cell2.CellStyle = styleSame; //3 ICell cell3 = row1.CreateCell(2); cell3.SetCellValue("請款部門:"); cell3.CellStyle = styleSame; //4 ICell cell4 = row1.CreateCell(3); cell4.SetCellValue(settlementDetailModels.facname); cell4.CellStyle = styleSame; //5 ICell cell5 = row1.CreateCell(4); cell5.SetCellValue("申請付款日期:"); cell5.CellStyle = styleSame; //6 ICell cell6 = row1.CreateCell(5); cell6.SetCellValue(settlementDetailModels.applytime.ToString()); cell6.CellStyle = styleSame; //7 ICell cell7 = row1.CreateCell(6); cell7.SetCellValue("結算總金額:"); cell7.CellStyle = styleSame; //8 ICell cell8 = row1.CreateCell(7); cell8.SetCellValue("" + settlementDetailModels.totalamount); cell8.CellStyle = styleSame; row1.Height = 30 * 20; #endregion 第一行資料 #region 第二行資料 IRow row2 = sheet.CreateRow(2); ICell row2Cell1 = row2.CreateCell(0); row2Cell1.SetCellValue("摘要:"); row2Cell1.CellStyle = styleSame; for (int p= 1; p < 8; p++) { ICell rowJrow2Cell1 = row2.CreateCell(p); rowJrow2Cell1.SetCellValue(""); rowJrow2Cell1.CellStyle = styleSame; } ICellStyle cellstyleNote1 = workbook.CreateCellStyle(); cellstyleNote1.BorderBottom = BorderStyle.Thin; cellstyleNote1.BorderLeft = BorderStyle.Thin; cellstyleNote1.BorderRight = BorderStyle.Thin; cellstyleNote1.BorderTop = BorderStyle.Thin; cellstyleNote1.Alignment = HorizontalAlignment.Left; cellstyleNote1.VerticalAlignment = VerticalAlignment.Center; cellstyleNote1.SetFont(fontSame); ICell row2Cell2 = row2.CreateCell(1); row2Cell2.SetCellValue(settlementDetailModels.note); row2Cell2.CellStyle = cellstyleNote1; row2.Height = 30 * 20; #endregion 第二行資料 #region 第三行 資料title IRow row3 = sheet.CreateRow(3); ICell row3Cell1 = row3.CreateCell(0); row3Cell1.SetCellValue("費用專案"); row3Cell1.CellStyle = styleSame; ICell row3Cell2 = row3.CreateCell(1); row3Cell2.SetCellValue("金額"); row3Cell2.CellStyle = styleSame; ICell row3Cell3 = row3.CreateCell(2); row3Cell3.SetCellValue("大寫金額"); row3Cell3.CellStyle = styleSame; ICell row3Cell4 = row3.CreateCell(3); row3Cell4.SetCellValue("支付方式"); row3Cell4.CellStyle = styleSame; ICell row3Cell5 = row3.CreateCell(4); row3Cell5.SetCellValue("收款方名稱"); row3Cell5.CellStyle = styleSame; ICell row3Cell6 = row3.CreateCell(5); row3Cell6.SetCellValue("賬號"); row3Cell6.CellStyle = styleSame; ICell row3Cell7 = row3.CreateCell(6); row3Cell7.SetCellValue("開戶行"); row3Cell7.CellStyle = styleSame; ICell row3Cell8 = row3.CreateCell(7); row3Cell8.SetCellValue("付款主體"); row3Cell8.CellStyle = styleSame; row3.Height = 30 * 20; #endregion 第三行 資料title int rowNumber = 4; #region 內容行 foreach (var r in settlementDetailModels.supplys) { IRow rowN = sheet.CreateRow(rowNumber); ICell rowNCell1 = rowN.CreateCell(0); rowNCell1.SetCellValue(r.project); rowNCell1.CellStyle = styleSame; ICell rowNCell2 = rowN.CreateCell(1); rowNCell2.SetCellValue(r.totalamount.ToString()); rowNCell2.CellStyle = styleSame; ICell rowNCell3 = rowN.CreateCell(2); rowNCell3.SetCellValue(DaXie(r.totalamount.ToString())); rowNCell3.CellStyle = styleSame; ICell rowNCell4 = rowN.CreateCell(3); rowNCell4.SetCellValue(r.paytypename); rowNCell4.CellStyle = styleSame; ICell rowNCell5 = rowN.CreateCell(4); rowNCell5.SetCellValue(r.collectusername); rowNCell5.CellStyle = styleSame; ICell rowNCell6 = rowN.CreateCell(5); rowNCell6.SetCellValue(getBankCardNumber(r.collectbankcard)); rowNCell6.CellStyle = styleSame; ICell rowNCell7 = rowN.CreateCell(6); rowNCell7.SetCellValue(r.collectbank); rowNCell7.CellStyle = styleSame; ICell rowNCell8 = rowN.CreateCell(7); rowNCell8.SetCellValue(settlementDetailModels.facname); rowNCell8.CellStyle = styleSame; rowN.Height = 30 * 20; rowNumber++; } #endregion 內容行 int numberSupply = settlementDetailModels.supplys.Count(); if (numberSupply < 6) { for (int i = 0; i < 7 - numberSupply; i++) { IRow rowI = sheet.CreateRow(rowNumber); for(int j = 0; j < 8; j++) { ICell rowJCell1 = rowI.CreateCell(j); rowJCell1.SetCellValue(""); rowJCell1.CellStyle = styleSame; } rowI.Height = 30 * 20; rowNumber++; } } int ammountNumber = rowNumber; #region 合計行 IRow rowAmount = sheet.CreateRow(rowNumber++); ICell rowAmountCell1 = rowAmount.CreateCell(0); rowAmountCell1.SetCellValue("合計"); rowAmountCell1.CellStyle = style; ICell rowAmountCell2 = rowAmount.CreateCell(1); rowAmountCell2.SetCellValue("" + settlementDetailModels.totalamount.ToString()); rowAmountCell2.CellStyle = style; ICell rowAmountCell3 = rowAmount.CreateCell(2); ICell rowAmountCell4 = rowAmount.CreateCell(3); rowAmountCell4.SetCellValue(DaXie(settlementDetailModels.totalamount.ToString())); rowAmountCell4.CellStyle = style; for (int j4 = 4; j4 < 8; j4++) { ICell rowJ4Cell1 = rowAmount.CreateCell(j4); rowJ4Cell1.SetCellValue(""); rowJ4Cell1.CellStyle = styleSame; } rowAmount.Height = 30 * 20; #endregion 合計行 int backRow = rowNumber; IRow rowBack = sheet.CreateRow(rowNumber++);//空白行 for (int j = 0; j < 8; j++) { ICell rowJrowBackCell1 = rowBack.CreateCell(j); rowJrowBackCell1.SetCellValue(""); rowJrowBackCell1.CellStyle = styleSame; } rowBack.Height = 30 * 20; #region 請款人 相關資訊 int applyRow = rowNumber; IRow rowApply = sheet.CreateRow(rowNumber++); ICell rowApplyCell1 = rowApply.CreateCell(0); rowApplyCell1.SetCellValue("請款人:"); rowApplyCell1.CellStyle = styleSame; ICell rowApplyCell2 = rowApply.CreateCell(1); rowApplyCell2.SetCellValue(""); rowApplyCell2.CellStyle = styleSame; ICell rowApplyCell3 = rowApply.CreateCell(2); rowApplyCell3.SetCellValue(""); rowApplyCell3.CellStyle = styleSame; ICell rowApplyCell4 = rowApply.CreateCell(3); rowApplyCell4.SetCellValue("部門領導稽核:"); rowApplyCell4.CellStyle = styleSame; ICell rowApplyCell5 = rowApply.CreateCell(4); rowApplyCell5.SetCellValue(""); rowApplyCell5.CellStyle = styleSame; ICell rowApplyCell6 = rowApply.CreateCell(5); rowApplyCell6.SetCellValue(""); rowApplyCell6.CellStyle = styleSame; ICell rowApplyCell7 = rowApply.CreateCell(6); rowApplyCell7.SetCellValue("財務稽核:"); rowApplyCell7.CellStyle = styleSame; ICell rowApplyCell8 = rowApply.CreateCell(7); rowApplyCell8.SetCellValue(""); rowApplyCell8.CellStyle = styleSame; rowApply.Height = 30 * 20; #endregion 請款人相關資訊 int descriptionTitleNum = rowNumber; IRow rowDescriptTitle = sheet.CreateRow(rowNumber++); ICell rowDescriptTitleCell1 = rowDescriptTitle.CreateCell(0); rowDescriptTitleCell1.SetCellValue("附件(請附發票,收據等)"); rowDescriptTitleCell1.CellStyle = styleSame; rowDescriptTitle.Height = 30 * 20; for (int jp =1; jp < 8; jp++) { ICell rowJrowDescriptTitleCell = rowDescriptTitle.CreateCell(jp); rowJrowDescriptTitleCell.SetCellValue(""); rowJrowDescriptTitleCell.CellStyle = styleSame; } #region 備註說明 int rowNoteNum = rowNumber; //備註行樣式 ICellStyle cellstyleNote = workbook.CreateCellStyle(); cellstyleNote.BorderBottom = BorderStyle.Thin; cellstyleNote.BorderLeft = BorderStyle.Thin; cellstyleNote.BorderRight = BorderStyle.Thin; cellstyleNote.BorderTop = BorderStyle.Thin; cellstyleNote.Alignment = HorizontalAlignment.Left; cellstyleNote.VerticalAlignment = VerticalAlignment.Center; cellstyleNote.WrapText = true; cellstyleNote.SetFont(fontSame); IRow rowNote = sheet.CreateRow(rowNumber++); ICell rowNoteCell = rowNote.CreateCell(0); rowNoteCell.SetCellValue(" 1.需要向公司申請對外支付時候填寫此表;\n 2.用途說明填寫:費用支出的實際用途,填寫大類即可(如:配件及精品、辦公用品、人力資源、裝修等);\n 3.務必填寫好支付方式:轉賬(首選)、支票和對方的收款賬號及銀行資訊。"); rowNoteCell.CellStyle = cellstyleNote; for (int jpy = 1; jpy < 8; jpy++) { ICell rowJrowNoteCell = rowNote.CreateCell(jpy); rowJrowNoteCell.SetCellValue(""); rowJrowNoteCell.CellStyle = styleSame; } rowNote.Height = 30 * 20; IRow rowNote1 = sheet.CreateRow(rowNumber++); ICell rowNote1Cell1 = rowNote1.CreateCell(0); rowNote1Cell1.SetCellValue(""); rowNote1Cell1.CellStyle = cellstyleNote; for (int jps = 1; jps < 8; jps++) { ICell rowJrowNote1Cell = rowNote1.CreateCell(jps); rowJrowNote1Cell.SetCellValue(""); rowJrowNote1Cell.CellStyle = styleSame; } rowNote1.Height = 30 * 20; IRow rowNote2 = sheet.CreateRow(rowNumber++); ICell rowNote2Cell1 = rowNote2.CreateCell(0); rowNote2Cell1.SetCellValue(""); rowNote2Cell1.CellStyle = cellstyleNote; for (int jps1 = 1; jps1 < 8; jps1++) { ICell rowJrowNote2Cell = rowNote2.CreateCell(jps1); rowJrowNote2Cell.SetCellValue(""); rowJrowNote2Cell.CellStyle = styleSame; } rowNote2.Height = 30 * 20; #endregion 備註說明 //需要 合併單元格 或合併行 //CellRangeAddress 四個引數為:起始行 結束行 起始列 結束列 sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 1, 7)); //合併第一行 從0單元格合併到第8個單元格 sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(2, 2, 1, 7));//合併第3行 從1單元格合併到第8個單元格 sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(ammountNumber, ammountNumber, 1, 2));//合併合計行的 第2個單元格和第3個單元格 sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(ammountNumber, ammountNumber, 3, 5));//合併合計行的 第4個單元格和第6個單元格 sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(applyRow, applyRow, 1, 2));//合併請款人行 合併第2單元格和第3單元格 sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(applyRow, applyRow, 4, 5));//合併請款人行 合併第5單元格 和第6單元格 sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(descriptionTitleNum, descriptionTitleNum, 0, 7)); sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(backRow, backRow, 0, 7));//合併空白行 從單元格 0 到8 個單元格 sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(rowNoteNum, rowNoteNum + 2, 0, 7));//合併 備註說明行 合併備註3行 即0-7列 //設定列的高度 sheet.SetColumnWidth(0, 18 * 256);//第0列的列寬為18 sheet.SetColumnWidth(1, 16 * 256); sheet.SetColumnWidth(2, 22 * 256); sheet.SetColumnWidth(3, 18 * 256); sheet.SetColumnWidth(4, 22 * 256); sheet.SetColumnWidth(5, 30 * 256); sheet.SetColumnWidth(6, 22 * 256); sheet.SetColumnWidth(7, 16* 256); #endregion 匯出excel 樣式設定 using (MemoryStream ms = new MemoryStream()) { workbook.Write(ms); string fileName = "申請付款單.xlsx"; #region 附加資訊 //文件摘要資訊 DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation(); dsi.Company = "xx公司申請付款單例項"; workbook.DocumentSummaryInformation = dsi; // SummaryInformation si = PropertySetFactory.CreateSummaryInformation(); si.Author = "申請付款單例項"; //填加xls檔案作者資訊 si.ApplicationName = "NPOI程式"; //填加xls檔案建立程式資訊 si.LastAuthor = "申請付款單例項"; //填加xls檔案最後儲存者資訊 si.Comments = "申請付款單例項所有"; //填加xls檔案作者資訊 si.Title = "申請付款單例項"; //填加xls檔案標題資訊 si.Subject = "申請付款單例項";//填加檔案主題資訊 si.CreateDateTime = DateTime.Now; workbook.SummaryInformation = si; #endregion string date = DateTime.Now.ToShortDateString(); //下載報表 var res = HttpContext.Current.Response; res.Clear(); res.Buffer = true; res.Charset = "GBK"; res.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8) + date + ".xls"); res.ContentEncoding = System.Text.Encoding.GetEncoding("GBK"); res.ContentType = "application/ms-excel;charset=GBK"; workbook.Write(res.OutputStream); res.Flush(); res.End(); } }
        /// <summary>
        /// 數字金額轉換為中文金額
        /// </summary>
        /// <param name="money"></param>
        /// <returns></returns>
        private  string DaXie(string money)
        {
            //將小寫金額轉換成大寫金額
            double MyNumber = Convert.ToDouble(money);
            String[] MyScale = { "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "" };
            String[] MyBase = { "", "", "", "", "", "", "", "", "", "" };
            String M = "";
            bool isPoint = false;
            if (money.IndexOf(".") != -1)
            {
                money = money.Remove(money.IndexOf("."), 1);
                isPoint = true;
            }
            for (int i = money.Length; i > 0; i--)
            {
                int MyData = Convert.ToInt16(money[money.Length - i].ToString());//?
                M += MyBase[MyData];//?
                if (isPoint == true)
                {
                    M += MyScale[i - 1];//?
                }
                else
                {
                    M += MyScale[i + 1];//?
                }
            }
            return M;
        }
        /// <summary>
        /// 銀行卡號增加空格
        /// </summary>
        /// <param name="bankCardNumber"></param>
        /// <returns></returns>
        private string getBankCardNumber(string bankCardNumber)
        {
            char []st = bankCardNumber.ToCharArray();
            string result = "";
            for(int i = 0; i < st.Length; i++)
            {
                if (i % 4 == 0)
                {
                    result += " " + st[i];
                }
                else
                {
                    result += st[i];
                }
            }
            return result;
        }