1,引用Aspose.Cells dll
2,using Aspose.Cells;
3,
Workbook excel = new Workbook(); string strFilePath = System.Web.HttpContext.Current.Server.MapPath("~/File/ExportTemplate/test.xls"); //建立excel并打开模板文件,这种先定义好模板,然后再在模板上进行填写数据 excel.Open(strFilePath); Worksheet sheet = excel.Worksheets["Sheet1"]; Style styleAlignment = excel.Styles[excel.Styles.Add()]; styleAlignment.VerticalAlignment = Aspose.Cells.TextAlignmentType.Top; styleAlignment.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Left; ///……………………………………………………这里省略获取数据代码———————— DataTable dtDetail = PubMethod.GetPagedData(strPro, htDetail, 1000000, 1).Tables[1]; //设置表头值 sheet.Cells[1, 0].PutValue("销税" + dtDetail.Rows[0]["DOME_NO"] + "号"); sheet.Cells[2, 0].PutValue("企业名称:" + strCompNo);//企业名称 sheet.Cells[2, 4].PutValue("加工贸易手册号:" + strHbNo);//加工贸易手册号 DataTable dtTmp = new DataTable(); for (int i = 0; i < 9; i++) { dtTmp.Columns.Add(i.ToString()); } for (int i = 0; i < dtDetail.Rows.Count; i++) { DataRow dr = dtTmp.NewRow(); dr[0] = dtDetail.Rows[i]["G_NO"]; dr[1] = dtDetail.Rows[i]["G_NAME"]; dr[2] = dtDetail.Rows[i]["G_CODE"]; dr[3] = dtDetail.Rows[i]["ELEMENT"]; dr[4] = dtDetail.Rows[i]["SUMQTY"]; dr[5] = dtDetail.Rows[i]["UNIT"]; dr[6] = dtDetail.Rows[i]["PRICE"]; dr[7] = dtDetail.Rows[i]["TOTAL_PRICE"]; dr[8] = dtDetail.Rows[i]["CURR"]; dtTmp.Rows.Add(dr); } dtTmp.AcceptChanges(); if (dtTmp.Rows.Count > 0) { sheet.Cells.ImportDataTable(dtTmp, false, 5, 0, dtTmp.Rows.Count, 9, false, "", true); } //合计栏位 sheet.Cells[dtDetail.Rows.Count + 5, 1].PutValue("合计:"); sheet.Cells[dtDetail.Rows.Count + 5, 1].Style.Font.IsBold = true; sheet.Cells[dtDetail.Rows.Count + 5, 1].Style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center; Decimal[] arrDecimal = { 0, 0 }; for (int i = 0; i < dtDetail.Rows.Count; i++) { arrDecimal[0] += Convert.ToDecimal(dtDetail.Rows[i]["SUMQTY"]); arrDecimal[1] += Convert.ToDecimal(dtDetail.Rows[i]["TOTAL_PRICE"]); } sheet.Cells[dtDetail.Rows.Count + 5, 4].PutValue(arrDecimal[0]); sheet.Cells[dtDetail.Rows.Count + 5, 7].PutValue(arrDecimal[1]); sheet.Cells[dtDetail.Rows.Count + 5, 4].Style.Font.IsBold = true; sheet.Cells[dtDetail.Rows.Count + 5, 7].Style.Font.IsBold = true; Style styleAlignment = excel.Styles[excel.Styles.Add()]; styleAlignment.VerticalAlignment = Aspose.Cells.TextAlignmentType.Top; styleAlignment.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Left; styleAlignment.Font.Size = 8; sheet.Cells[dtDetail.Rows.Count + 6, 0].PutValue("加工贸易部门批注意见:"); ///设高度 sheet.Cells.SetRowHeight(dtDetail.Rows.Count + 8, 10); sheet.Cells.SetRowHeight(dtDetail.Rows.Count + 9, 10); /// *******这里很重要,非学重要***: /// 这里要+8,+9两行合并一下,然后下面只对+9和+11行进行样式调整System.Drawing.Color.White,让它的线变白。 /// 这样才会有“注:本表格……”上面那条线,如果不合并两行,就用一行,就会发现这条线一直不出现。 Range r2 = sheet.Cells.CreateRange(sheet.Cells[dtDetail.Rows.Count + 8, 0].Name, sheet.Cells[dtDetail.Rows.Count + 9, 8].Name); r2.Merge(); //样式调整,把黑边线去掉 for (int i = 1; i < 8; i++) { sheet.Cells[dtDetail.Rows.Count + 6, i].Style.Borders.SetColor(System.Drawing.Color.White); sheet.Cells[dtDetail.Rows.Count + 7, i].Style.Borders.SetColor(System.Drawing.Color.White); sheet.Cells[dtDetail.Rows.Count + 8, i].Style.Borders.SetColor(System.Drawing.Color.White); } //自适应行高 sheet.AutoFitRows(); Style styleLeft = excel.Styles[excel.Styles.Add()]; styleLeft.Borders[BorderType.LeftBorder].Color = System.Drawing.Color.Black; styleLeft.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin; Style styleRight = excel.Styles[excel.Styles.Add()]; styleRight.Borders[BorderType.RightBorder].Color = System.Drawing.Color.Black; styleRight.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin; sheet.Cells[dtDetail.Rows.Count + 6, 0].Style = styleLeft; sheet.Cells[dtDetail.Rows.Count + 6, 0].Style.Font.Size = 8; sheet.Cells.SetRowHeight(dtDetail.Rows.Count + 6, 110); sheet.Cells[dtDetail.Rows.Count + 6, 0].Style.VerticalAlignment = Aspose.Cells.TextAlignmentType.Top; sheet.Cells[dtDetail.Rows.Count + 7, 0].Style = styleLeft; sheet.Cells[dtDetail.Rows.Count + 8, 0].Style = styleLeft; sheet.Cells[dtDetail.Rows.Count + 7, 4].PutValue("签字:"); sheet.Cells[dtDetail.Rows.Count + 7, 7].PutValue("日期:"); sheet.Cells[dtDetail.Rows.Count + 7, 4].Style = styleAlignment; sheet.Cells[dtDetail.Rows.Count + 7, 7].Style = styleAlignment; sheet.Cells[dtDetail.Rows.Count + 6, 8].Style = styleRight; sheet.Cells[dtDetail.Rows.Count + 7, 8].Style = styleRight; sheet.Cells[dtDetail.Rows.Count + 8, 8].Style = styleRight; //单元格外文字设置 Range r6 = sheet.Cells.CreateRange(sheet.Cells[dtDetail.Rows.Count + 10, 0].Name, sheet.Cells[dtDetail.Rows.Count + 10, 8].Name); r6.Merge(); sheet.Cells[dtDetail.Rows.Count + 10, 0].PutValue("注:本表格一式三份: 一份通关部门留存, 一份加工贸易部门留存, 一份企业留存."); sheet.Cells[dtDetail.Rows.Count + 10, 0].Style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Left; for (int i = 0; i < 9; i++) { sheet.Cells[dtDetail.Rows.Count + 10, i].Style.Borders.SetColor(System.Drawing.Color.White); }
4,上面的代码最后显示的格式是这样的,见下图:
5,最终转民PDF打印是这样的,见下图:
6,注意:这边做样式的时候一定要注意不要将上面的样式给覆盖了。
如果对+6,0已经做了一些样式,然后再用下面这个语句,+6,0的样式将被改写,样式就只能是styleLeft对应样式了。
sheet.Cells[dtDetail.Rows.Count + 6, 0].Style = styleLeft;
7,增加勾选框
到目前为止我看网上没有特别好的解决方法,有些人说直接插入一个图片,那么有兴趣的同学可以去试一下。
做了之后你们发现,如果你用模板做的你插入的那个图片就是浮在excel里面。如果你最终要以pdf打印出来,那么你会发现样式没法调,很不好看。
好那么我这边用一个很简单的方法让大家实现文本框加勾。
一,首先我做好一个excel打印的模板,然后在右边(非打印区域)增加待插入内容。
内容:输入一个R,对就是大写的R,然后字体设成“Wingdings 2” 然后再写上“是”和插入一个特殊字符(没勾选的框),再写上一个否最终就是(设这个放在3,18单元格中)
当然还要在另一个单元格里做一个选择否的。(设这个放在3,19单元格中)
二,根据数据中实际勾选内容,将第一步对应的单元格复制到你要填入值的单元格中。
例:现在的要实际选择了“是”,且要将值放到cell[3,8]中,那么代码如下:
sheet.Cells[3, 8].Copy(sheet.Cells[3,18]);
三,然后最后再用代码将第一步增加的待插入内容,这样打印出来就是我们要的那个模板的样式。且有勾选框。
7,Aspose.Cells如何设置单元格数字和日期的显示格式
//Getting the Style of the A1 Cell
Style style = worksheet.Cells["A1"].GetStyle();
//Setting the display format to number 15 to show date as "d-mmm-yy"
style.Number = 15;
Aspose.Cells提供的内嵌显示格式: Value Type Format String 0 General General 1 Decimal 0 2 Decimal 0.00 3 Decimal #,##0 4 Decimal #,##0.00 5 Currency $#,##0;$-#,##0 6 Currency $#,##0;[Red]$-#,##0 7 Currency $#,##0.00;$-#,##0.00 8 Currency $#,##0.00;[Red]$-#,##0.00 9 Percentage 0% 10 Percentage 0.00% 11 Scientific 0.00E+00 12 Fraction # ?/? 13 Fraction # / 14 Date m/d/yy 15 Date d-mmm-yy 16 Date d-mmm 17 Date mmm-yy 18 Time h:mm AM/PM 19 Time h:mm:ss AM/PM 20 Time h:mm 21 Time h:mm:ss 22 Time m/d/yy h:mm 37 Currency #,##0;-#,##0 38 Currency #,##0;[Red]-#,##0 39 Currency #,##0.00;-#,##0.00 40 Currency #,##0.00;[Red]-#,##0.00 41 Accounting _ * #,##0_ ;_ * "_ ;_ @_ 42 Accounting _ $* #,##0_ ;_ $* "_ ;_ @_ 43 Accounting _ * #,##0.00_ ;_ * "??_ ;_ @_ 44 Accounting _ $* #,##0.00_ ;_ $* "??_ ;_ @_ 45 Time mm:ss 46 Time h :mm:ss 47 Time mm:ss.0 48 Scientific ##0.0E+00 49 Text @