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 @