| | |
| | | using DataTable = System.Data.DataTable; |
| | | using IFont = NPOI.SS.UserModel.IFont; |
| | | |
| | | |
| | | namespace CommonHelper |
| | | { |
| | | /// <summary> |
| | |
| | | /// </summary> |
| | | /// <param name="dt"></param> |
| | | /// <param name="file"></param> |
| | | public static void FRMExportExcel(DataTable dt, string file,string title) |
| | | public static void FRMExportExcel(DataTable dt, string file, string title) |
| | | { |
| | | IWorkbook workbook; |
| | | string fileExt = Path.GetExtension(file).ToLower(); |
| | |
| | | |
| | | //表头 |
| | | IRow row = sheet.CreateRow(0); |
| | | row.Height = 25 * 20; //行高为30 |
| | | // sheet.SetColumnWidth(0, 13 * 256); |
| | | CellRangeAddress region = new CellRangeAddress(0, 0, 0, dt.Columns.Count-1); |
| | | row.Height = 25 * 20; //行高为30 |
| | | // sheet.SetColumnWidth(0, 13 * 256); |
| | | CellRangeAddress region = new CellRangeAddress(0, 0, 0, dt.Columns.Count - 1); |
| | | sheet.SetColumnWidth(0, 30 * 256); |
| | | sheet.AddMergedRegion(region); |
| | | #region 标题样式 |
| | | ICellStyle style = workbook.CreateCellStyle();//创建样式对象 |
| | | IFont font = workbook.CreateFont(); //创建一个字体样式对象 |
| | | font.FontName = "微软雅黑"; //和excel里面的字体对应 |
| | | // font.Color = new HSSFColor.Pink().GetIndex();//颜色参考NPOI的颜色对照表(替换掉PINK()) |
| | | // font.Color = new HSSFColor.Pink().GetIndex();//颜色参考NPOI的颜色对照表(替换掉PINK()) |
| | | font.IsItalic = false; //斜体 |
| | | font.FontHeightInPoints = 14;//字体大小 |
| | | font.Boldweight = short.MaxValue;//字体加粗 |
| | |
| | | #endregion |
| | | #region 内容样式 1 右对齐 千位符保留两位小数 |
| | | ICellStyle style1 = workbook.CreateCellStyle(); |
| | | IFont font1 = workbook.CreateFont(); |
| | | IFont font1 = workbook.CreateFont(); |
| | | var format1 = workbook.CreateDataFormat(); |
| | | font1.FontName = "微软雅黑"; |
| | | font1.IsItalic = false; |
| | | font1.FontName = "微软雅黑"; |
| | | font1.IsItalic = false; |
| | | font1.FontHeightInPoints = 12; |
| | | font1.Boldweight = short.MaxValue; |
| | | style1.SetFont(font1); |
| | |
| | | style1.WrapText = true;//设置自动换行 |
| | | style1.Alignment = HorizontalAlignment.Right; |
| | | #endregion |
| | | |
| | | |
| | | #region 内容样式2 左对齐 |
| | | ICellStyle style2 = workbook.CreateCellStyle(); |
| | | IFont font2 = workbook.CreateFont(); |
| | | font2.FontName = "微软雅黑"; |
| | | font2.IsItalic = false; |
| | | IFont font2 = workbook.CreateFont(); |
| | | font2.FontName = "微软雅黑"; |
| | | font2.IsItalic = false; |
| | | font2.FontHeightInPoints = 12; |
| | | font2.Boldweight = short.MaxValue; |
| | | style2.SetFont(font2); |
| | | style2.SetFont(font2); |
| | | style2.BorderTop = BorderStyle.Thin; |
| | | style2.BorderBottom = BorderStyle.Thin; |
| | | style2.BorderLeft = BorderStyle.Thin; |
| | |
| | | { |
| | | continue; |
| | | } |
| | | |
| | | |
| | | int OldHg = row1.Height; |
| | | for (int z= 0; z < dt.Columns.Count; z++) |
| | | for (int z = 0; z < dt.Columns.Count; z++) |
| | | { |
| | | if (row1.GetCell(z) == null) |
| | | { |
| | |
| | | //row1.Height = 36 * 20; |
| | | for (int j = 0; j < dt.Columns.Count; j++) |
| | | { |
| | | |
| | | dt.Rows[i][j] = dt.Rows[i][j].ToString().Replace("wqeewqsdad","").Replace( "--------", "*").Replace("木部","本部").Replace("贷币", "货币"); |
| | | |
| | | dt.Rows[i][j] = dt.Rows[i][j].ToString().Replace("wqeewqsdad", "").Replace("--------", "*").Replace("木部", "本部").Replace("贷币", "货币"); |
| | | if (i == 0) |
| | | { |
| | | if (dt.Rows[i][j].ToString() == "" || dt.Rows[i][j].ToString().Length > 5) |
| | |
| | | } |
| | | } |
| | | } |
| | | if (dt.Columns.Count==4&&i == 0) |
| | | if (dt.Columns.Count == 4 && i == 0) |
| | | { |
| | | if (dt.Rows[i][j].ToString() == "") |
| | | { |
| | | if (j==0) |
| | | if (j == 0) |
| | | { |
| | | dt.Rows[i][j] = "项目"; |
| | | } |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | |
| | | ICell cell = row1.CreateCell(j); |
| | | if (j == 0) |
| | | { |
| | | if ((dt.Rows[i][j].ToString().Contains("流动资产") && dt.Rows[i][j].ToString().Length < 6 )||( dt.Rows[i][j].ToString().Contains("非流动资产"))&& dt.Rows[i][j].ToString().Length<7|| dt.Rows[i][j].ToString().Contains("合计")|| dt.Rows[i][j].ToString().Contains("总计")) |
| | | if ((dt.Rows[i][j].ToString().Contains("流动资产") && dt.Rows[i][j].ToString().Length < 6) || (dt.Rows[i][j].ToString().Contains("非流动资产")) && dt.Rows[i][j].ToString().Length < 7 || dt.Rows[i][j].ToString().Contains("合计") || dt.Rows[i][j].ToString().Contains("总计")) |
| | | { |
| | | FontFlag = true; |
| | | cell.CellStyle = style3; |
| | |
| | | { |
| | | cell.CellStyle = style2; |
| | | } |
| | | |
| | | |
| | | } |
| | | else |
| | | { |
| | |
| | | { |
| | | cell.CellStyle = style1; |
| | | } |
| | | |
| | | |
| | | } |
| | | double Money = -1; |
| | | if (double.TryParse(dt.Rows[i][j].ToString(),out Money)) |
| | | if (double.TryParse(dt.Rows[i][j].ToString(), out Money)) |
| | | { |
| | | cell.SetCellValue(double.Parse(dt.Rows[i][j].ToString())); |
| | | } |
| | |
| | | { |
| | | cell.SetCellValue(dt.Rows[i][j].ToString()); |
| | | } |
| | | |
| | | |
| | | } |
| | | } |
| | | |
| | |
| | | } |
| | | } |
| | | #region 导出报表 |
| | | public static void GenerateReportExcel(string TempletFilePath, string ReportFilePath, string SheetName, DataTable dt,string ReportType, int MaxColumn) |
| | | public static void GenerateReportExcel(string TempletFilePath, string ReportFilePath, string SheetName, DataTable dt, string ReportType, int MaxColumn) |
| | | { |
| | | using (FileStream ExcelFileStream = new FileStream(TempletFilePath, FileMode.Open, FileAccess.Read)) |
| | | { |
| | |
| | | } |
| | | |
| | | } |
| | | else if(SheetName== "现金流量表") |
| | | else if (SheetName == "现金流量表") |
| | | { |
| | | if (ReportType == "合并报表") |
| | | { |
| | |
| | | } |
| | | |
| | | } |
| | | else if (SheetName=="现金流量表补充资料") |
| | | else if (SheetName == "现金流量表补充资料") |
| | | { |
| | | if (ReportType== "合并报表") |
| | | if (ReportType == "合并报表") |
| | | { |
| | | SheetName = "现金流量表补充资料-合并"; |
| | | } |
| | |
| | | } |
| | | SheetRow = 32; |
| | | } |
| | | else if (SheetName== "所有者权益变动表") |
| | | else if (SheetName == "所有者权益变动表") |
| | | { |
| | | if (ReportType == "合并报表") |
| | | { |
| | |
| | | { |
| | | for (int j = 0; j < MaxColumn; j++) |
| | | { |
| | | if (ReportType=="合并报表") |
| | | if (ReportType == "合并报表") |
| | | { |
| | | if (j == 0 || j == 15) |
| | | { |
| | |
| | | } |
| | | } |
| | | } |
| | | |
| | | } |
| | | |
| | | } |
| | | } |
| | | else |
| | | { |
| | |
| | | #endregion |
| | | } |
| | | var Num = SheetRow - dt.Rows.Count; |
| | | if (Num>0) |
| | | if (Num > 0) |
| | | { |
| | | for (int i = dt.Rows.Count; i <= SheetRow; i++) |
| | | { |
| | |
| | | for (int j = 0; j < MaxColumn; j++) |
| | | { |
| | | ICell cell = row.GetCell(j); |
| | | if (cell!=null) |
| | | if (cell != null) |
| | | { |
| | | row.RemoveCell(cell); |
| | | } |
| | | |
| | | |
| | | } |
| | | |
| | | } |
| | |
| | | using (FileStream ExcelFileStream = new FileStream(TempletFilePath, FileMode.Open, FileAccess.Read)) |
| | | { |
| | | IWorkbook workbook = NPOI.SS.UserModel.WorkbookFactory.Create(ExcelFileStream); |
| | | |
| | | |
| | | ISheet sheet1 = workbook.GetSheet(SheetName); |
| | | //sheet1.ProtectSheet(SheetLockPassword); |
| | | ISheet sheetNote = workbook.GetSheet("填写说明"); |
| | |
| | | // return Result; |
| | | //} |
| | | |
| | | |
| | | } |
| | | } |