纽威
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

596 lines
27 KiB

3 years ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.IO;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Web;
  9. using System.Web.UI;
  10. using System.Web.UI.WebControls;
  11. using NFine.Code.Excel;
  12. using Aspose.Cells;
  13. using NPOI.HSSF.UserModel;
  14. using NPOI.SS.UserModel;
  15. using NPOI.HPSF;
  16. using System.Drawing;
  17. namespace NFine.Code
  18. {
  19. public class ExcelHelper
  20. {
  21. /// <summary>
  22. /// 导出数据到EXCEL文件
  23. /// </summary>
  24. /// <param name="page">页面</param>
  25. /// <param name="gvExcel">导出的GrivView</param>
  26. public static void ExportToExcel(System.Web.UI.Page page, GridView dgExcel, HttpResponse Response, string fileName)
  27. {
  28. try
  29. {
  30. fileName = fileName + ".xls";
  31. page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
  32. Response.Clear();
  33. Response.Buffer = true;
  34. Response.Charset = "utf-8";
  35. Response.Write("<meta http-equiv=Content-Type content=text/html;charset=utf-8>");
  36. Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
  37. Response.ContentType = "application/ms-excel";
  38. dgExcel.Page.EnableViewState = false;
  39. System.IO.StringWriter tw = new System.IO.StringWriter();
  40. System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(tw);
  41. dgExcel.RenderControl(hw);
  42. // 输出DataGrid内容
  43. Response.Write(tw.ToString());
  44. Response.End();
  45. }
  46. catch (Exception ex)
  47. {
  48. throw ex;
  49. }
  50. }
  51. }
  52. /// <summary>
  53. /// 导出EXCEL
  54. /// </summary>
  55. public class GridViewExportUtil
  56. {
  57. /// <summary>
  58. /// 将GRIDVIEW导出excel
  59. /// </summary>
  60. /// <param name="fileName">excel文件名</param>
  61. /// <param name="gv">GridView ID</param>
  62. public static void Export(GridView gv, string PreSellNo)
  63. {
  64. //dataGridView1.Columns[0].HeaderText 循环列判断是否要设置为数字类型
  65. List<int> list = new List<int>();
  66. int k = 0; //序号
  67. int p = 0; //价格
  68. int a = 0; //数量
  69. int d = 0; //折扣
  70. for (int i = 0; i < gv.HeaderRow.Cells.Count; i++)
  71. {
  72. //if (gv.HeaderRow.Cells[i].Text.Contains("F_Id") || gv.HeaderRow.Cells[i].Text.Contains("Product_Id"))
  73. //{
  74. // list.Add(i);
  75. //}
  76. if (gv.HeaderRow.Cells[i].Text.Contains("No.") || gv.HeaderRow.Cells[i].Text.Contains("Finished Weight(ct)") ||
  77. gv.HeaderRow.Cells[i].Text.Contains("Rough Weight(ct)") || gv.HeaderRow.Cells[i].Text.Contains("Price(USD)") ||
  78. gv.HeaderRow.Cells[i].Text.Contains("Amount(USD)"))
  79. {
  80. list.Add(i);
  81. }
  82. //if (gv.HeaderRow.Cells[i].Text.Contains("No."))
  83. // k = i;
  84. if (gv.HeaderRow.Cells[i].Text.Contains("Price(USD)"))
  85. p = i;
  86. if (gv.HeaderRow.Cells[i].Text.Contains("Amount(USD)"))
  87. a = i;
  88. //if (gv.HeaderRow.Cells[i].Text.Contains("Discount(%)"))
  89. // d = i;
  90. }
  91. foreach (GridViewRow row in gv.Rows)
  92. {
  93. int j = 0;
  94. foreach (TableCell cell in row.Cells)
  95. {
  96. if (!list.Contains(j))
  97. {
  98. cell.Style.Add("vnd.ms-excel.numberformat", "@");//给表格内容设置样式
  99. } // end foreach (TableCell cell in row.Cells)
  100. else
  101. {
  102. if (j != 0)
  103. {
  104. if (j == p || j == a)
  105. cell.Style.Add("vnd.ms-excel.numberformat", "#,##0.00");
  106. //else if (j == d)
  107. // cell.Style.Add("vnd.ms-excel.numberformat", "#0%");
  108. else
  109. cell.Style.Add("vnd.ms-excel.numberformat", "#,##0.000");//保留三位小数的样式,否则为默认样式
  110. }
  111. }
  112. j++;
  113. }
  114. } // end foreach (GridViewRow row in dgExcel.Rows)
  115. HttpContext.Current.Response.Clear();
  116. string styleText = @"<style> .text{mso-number-format:\@;} </style> ";
  117. string fileName = "";
  118. if (!string.IsNullOrEmpty(PreSellNo))
  119. fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + "-OrderNo[" + PreSellNo + "].xls";
  120. else
  121. fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
  122. HttpContext.Current.Response.AddHeader(
  123. "content-disposition", string.Format("attachment; filename={0}", fileName));
  124. HttpContext.Current.Response.ContentType = "application/ms-excel";
  125. HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");
  126. using (StringWriter sw = new StringWriter())
  127. {
  128. using (HtmlTextWriter htw = new HtmlTextWriter(sw))
  129. {
  130. // Create a form to contain the grid
  131. Table table = new Table();
  132. table.GridLines = gv.GridLines;
  133. if (gv.HeaderRow != null)
  134. {
  135. GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
  136. table.Rows.Add(gv.HeaderRow);
  137. for (int i = 0; i < gv.HeaderRow.Cells.Count; i++)
  138. {
  139. table.Rows[0].Cells[i].BackColor = System.Drawing.Color.FromArgb(220, 240, 241);
  140. table.Rows[0].Cells[i].ForeColor = System.Drawing.Color.Black;
  141. table.Rows[0].Cells[i].Height = Unit.Pixel(24);
  142. }
  143. }
  144. // add each of the data rows to the table
  145. foreach (GridViewRow row in gv.Rows)
  146. {
  147. GridViewExportUtil.PrepareControlForExport(row);
  148. row.Height = Unit.Pixel(22);
  149. row.HorizontalAlign = HorizontalAlign.Center;
  150. table.Rows.Add(row);
  151. }
  152. // add the footer row to the table
  153. if (gv.FooterRow != null)
  154. {
  155. GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
  156. table.Rows.Add(gv.FooterRow);
  157. }
  158. #region 手动增加头部三行和底部一行
  159. #region ItemList表头行
  160. table.Rows.AddAt(0, new TableRow());
  161. table.Rows[0].Cells.Add(new TableCell());
  162. table.Rows[0].Cells[0].Text = "ITEMS LIST";
  163. table.Rows[0].Cells[0].Font.Bold = true;
  164. table.Rows[0].Cells[0].ColumnSpan = 9;
  165. table.Rows[0].Cells[0].BackColor = System.Drawing.Color.FromArgb(220, 240, 241);
  166. table.Rows[0].Cells[0].ForeColor = System.Drawing.Color.Black;
  167. table.Rows[0].Cells[0].Height = Unit.Pixel(24);
  168. table.Rows[0].Cells[0].Font.Size = FontUnit.Parse("16");
  169. table.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
  170. table.Rows[0].Cells[0].BorderWidth = 0;
  171. table.Rows[0].BorderStyle = System.Web.UI.WebControls.BorderStyle.None;
  172. #endregion
  173. #region To S/C No 行
  174. table.Rows.AddAt(1, new TableRow());
  175. table.Rows[1].Cells.Add(new TableCell());
  176. table.Rows[1].Cells[0].Text = "TO:";
  177. table.Rows[1].Cells[0].Font.Bold = true;
  178. table.Rows[1].Cells[0].ColumnSpan = 4;
  179. table.Rows[1].Cells[0].HorizontalAlign = HorizontalAlign.Left;
  180. table.Rows[1].Cells[0].BorderWidth = 0;
  181. table.Rows[1].Cells.Add(new TableCell());
  182. table.Rows[1].Cells[1].Text = "";
  183. table.Rows[1].Cells[1].ColumnSpan = 2;
  184. table.Rows[1].Cells[1].BorderWidth = 0;
  185. table.Rows[1].Cells.Add(new TableCell());
  186. table.Rows[1].Cells[2].Text = "S/C NO:";
  187. table.Rows[1].Cells[2].Font.Bold = true;
  188. table.Rows[1].Cells[2].ColumnSpan = 3;
  189. table.Rows[1].Cells[2].HorizontalAlign = HorizontalAlign.Left;
  190. table.Rows[1].Cells[2].BorderWidth = 0;
  191. #endregion
  192. #region Date 行
  193. table.Rows.AddAt(2, new TableRow());
  194. table.Rows[2].Cells.Add(new TableCell());
  195. table.Rows[2].Cells[0].Text = "";
  196. table.Rows[2].Cells[0].ColumnSpan = 4;
  197. table.Rows[2].Cells[0].HorizontalAlign = HorizontalAlign.Left;
  198. table.Rows[2].Cells[0].BorderWidth = 0;
  199. table.Rows[2].Cells.Add(new TableCell());
  200. table.Rows[2].Cells[1].Text = "";
  201. table.Rows[2].Cells[1].ColumnSpan = 2;
  202. table.Rows[2].Cells[1].BorderWidth = 0;
  203. table.Rows[2].Cells.Add(new TableCell());
  204. table.Rows[2].Cells[2].Text = "DATE:" + DateTime.Now.ToString("yyyy-MM-dd");
  205. table.Rows[2].Cells[2].Font.Bold = true;
  206. table.Rows[2].Cells[2].ColumnSpan = 3;
  207. table.Rows[2].Cells[2].HorizontalAlign = HorizontalAlign.Left;
  208. table.Rows[2].Cells[2].BorderWidth = 0;
  209. #endregion
  210. #region Total 行
  211. table.Rows.Add(new TableRow());
  212. table.Rows[table.Rows.Count - 1].Cells.Add(new TableCell());
  213. table.Rows[table.Rows.Count - 1].Cells[0].Text = "Total:";
  214. table.Rows[table.Rows.Count - 1].Cells[0].ColumnSpan = 2;
  215. table.Rows[table.Rows.Count - 1].Cells[0].HorizontalAlign = HorizontalAlign.Left;
  216. table.Rows[table.Rows.Count - 1].Cells.Add(new TableCell());
  217. table.Rows[table.Rows.Count - 1].Cells[1].Text = "";
  218. table.Rows[table.Rows.Count - 1].Cells.Add(new TableCell());
  219. table.Rows[table.Rows.Count - 1].Cells[2].Text = "";
  220. table.Rows[table.Rows.Count - 1].Cells.Add(new TableCell());
  221. table.Rows[table.Rows.Count - 1].Cells[3].Text = "";
  222. table.Rows[table.Rows.Count - 1].Cells.Add(new TableCell());
  223. table.Rows[table.Rows.Count - 1].Cells[4].Text = "";
  224. table.Rows[table.Rows.Count - 1].Cells.Add(new TableCell());
  225. table.Rows[table.Rows.Count - 1].Cells[5].Text = "";
  226. table.Rows[table.Rows.Count - 1].Cells.Add(new TableCell());
  227. table.Rows[table.Rows.Count - 1].Cells[6].Text = "";
  228. table.Rows[table.Rows.Count - 1].Cells.Add(new TableCell());
  229. table.Rows[table.Rows.Count - 1].Cells[7].Text = "";
  230. #endregion
  231. #endregion
  232. //设置table整体字体样式和大小
  233. table.Font.Name = "宋体";
  234. table.Font.Size = FontUnit.Parse("9.5");
  235. // render the table into the htmlwriter
  236. table.RenderControl(htw);
  237. // render the htmlwriter into the response
  238. HttpContext.Current.Response.Write(sw.ToString());
  239. HttpContext.Current.Response.Write(styleText);
  240. HttpContext.Current.Response.End();
  241. }
  242. }
  243. }
  244. public static void Export2(GridView gv)
  245. {
  246. //dataGridView1.Columns[0].HeaderText 循环列判断是否要设置为数字类型
  247. List<int> list = new List<int>();
  248. int k = 0; //序号
  249. int p = 0; //价格
  250. for (int i = 0; i < gv.HeaderRow.Cells.Count; i++)
  251. {
  252. if (gv.HeaderRow.Cells[i].Text.Contains("No.") || gv.HeaderRow.Cells[i].Text.Contains("Finished Weight(ct)") ||
  253. gv.HeaderRow.Cells[i].Text.Contains("Rough Weight(ct)") || gv.HeaderRow.Cells[i].Text.Contains("Price(USD)")
  254. || gv.HeaderRow.Cells[i].Text.Contains("Qty"))
  255. {
  256. list.Add(i);
  257. }
  258. if (gv.HeaderRow.Cells[i].Text.Contains("Price(USD)"))
  259. p = i;
  260. }
  261. foreach (GridViewRow row in gv.Rows)
  262. {
  263. int j = 0;
  264. foreach (TableCell cell in row.Cells)
  265. {
  266. if (!list.Contains(j))
  267. {
  268. cell.Style.Add("vnd.ms-excel.numberformat", "@");//给表格内容设置样式
  269. } // end foreach (TableCell cell in row.Cells)
  270. else
  271. {
  272. if (j != 0)
  273. {
  274. if (j == p)
  275. cell.Style.Add("vnd.ms-excel.numberformat", "#,##0.00");
  276. else
  277. cell.Style.Add("vnd.ms-excel.numberformat", "#,##0.000");//保留三位小数的样式,否则为默认样式
  278. }
  279. }
  280. j++;
  281. }
  282. } // end foreach (GridViewRow row in dgExcel.Rows)
  283. HttpContext.Current.Response.Clear();
  284. string styleText = @"<style> .text{mso-number-format:\@;} </style> ";
  285. string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
  286. HttpContext.Current.Response.AddHeader(
  287. "content-disposition", string.Format("attachment; filename={0}", fileName));
  288. HttpContext.Current.Response.ContentType = "application/ms-excel";
  289. HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=utf-8\">");
  290. using (StringWriter sw = new StringWriter())
  291. {
  292. using (HtmlTextWriter htw = new HtmlTextWriter(sw))
  293. {
  294. // Create a form to contain the grid
  295. Table table = new Table();
  296. table.GridLines = gv.GridLines;
  297. if (gv.HeaderRow != null)
  298. {
  299. GridViewExportUtil.PrepareControlForExport(gv.HeaderRow);
  300. table.Rows.Add(gv.HeaderRow);
  301. for (int i = 0; i < gv.HeaderRow.Cells.Count; i++)
  302. {
  303. table.Rows[0].Cells[i].BackColor = System.Drawing.Color.FromArgb(220, 240, 241);
  304. table.Rows[0].Cells[i].ForeColor = System.Drawing.Color.Black;
  305. table.Rows[0].Cells[i].Height = Unit.Pixel(24);
  306. }
  307. }
  308. // add each of the data rows to the table
  309. foreach (GridViewRow row in gv.Rows)
  310. {
  311. GridViewExportUtil.PrepareControlForExport(row);
  312. row.Height = Unit.Pixel(22);
  313. row.HorizontalAlign = HorizontalAlign.Center;
  314. table.Rows.Add(row);
  315. }
  316. // add the footer row to the table
  317. if (gv.FooterRow != null)
  318. {
  319. GridViewExportUtil.PrepareControlForExport(gv.FooterRow);
  320. table.Rows.Add(gv.FooterRow);
  321. }
  322. //设置table整体字体样式和大小
  323. table.Font.Name = "宋体";
  324. table.Font.Size = FontUnit.Parse("9.5");
  325. // render the table into the htmlwriter
  326. table.RenderControl(htw);
  327. // render the htmlwriter into the response
  328. HttpContext.Current.Response.Write(sw.ToString());
  329. HttpContext.Current.Response.Write(styleText);
  330. HttpContext.Current.Response.End();
  331. }
  332. }
  333. }
  334. public static void Export(DataTable dt, string PreSellNo)
  335. {
  336. using (GridView gv = new GridView())
  337. {
  338. DataTable dtExport = new DataTable();
  339. dtExport.Columns.Add("No.", Type.GetType("System.Int32"));
  340. dtExport.Columns.Add("Product No.", Type.GetType("System.String"));
  341. dtExport.Columns.Add("Measurement", Type.GetType("System.String"));
  342. dtExport.Columns.Add("Finished Weight(ct)", Type.GetType("System.Decimal"));
  343. dtExport.Columns.Add("Rough Weight(ct)", Type.GetType("System.Decimal"));
  344. dtExport.Columns.Add("Price(USD)", Type.GetType("System.Decimal"));
  345. dtExport.Columns.Add("Discount(%)", Type.GetType("System.String"));
  346. dtExport.Columns.Add("Amount(USD)", Type.GetType("System.Decimal"));
  347. dtExport.Columns.Add("Shape", Type.GetType("System.String"));
  348. int number = 1;
  349. //for (int i = 0; i < 3; i++)
  350. //{
  351. // DataRow rtop = dtExport.NewRow();
  352. // dtExport.Rows.Add(rtop);
  353. //}
  354. foreach (DataRow row in dt.Rows)
  355. {
  356. DataRow r = dtExport.NewRow();
  357. r["No."] = number;
  358. r["Product No."] = row["ProductSN"].ToString();
  359. r["Measurement"] = row["Measurement"].ToString();
  360. r["Finished Weight(ct)"] = row["Weight"].ToString();
  361. r["Rough Weight(ct)"] = row["GrossWeight"].ToString();
  362. r["Price(USD)"] = string.IsNullOrEmpty(row["Price"].ToString()) ? 0 : decimal.Parse(row["Price"].ToString());
  363. r["Discount(%)"] = row["Discount"].ToString();
  364. r["Amount(USD)"] = string.IsNullOrEmpty(row["Amount"].ToString()) ? 0 : decimal.Parse(row["Amount"].ToString());
  365. r["Shape"] = row["Shape"].ToString();
  366. dtExport.Rows.Add(r);
  367. number++;
  368. }
  369. gv.DataSource = dtExport;
  370. gv.DataBind();
  371. Export(gv, PreSellNo);
  372. }
  373. }
  374. public static void Export2(DataTable dt)
  375. {
  376. using (GridView gv = new GridView())
  377. {
  378. DataTable dtExport = new DataTable();
  379. dtExport.Columns.Add("No.", Type.GetType("System.Int32"));
  380. dtExport.Columns.Add("Product No.", Type.GetType("System.String"));
  381. dtExport.Columns.Add("Measurement", Type.GetType("System.String"));
  382. dtExport.Columns.Add("Sort", Type.GetType("System.String"));
  383. dtExport.Columns.Add("Location", Type.GetType("System.String"));
  384. dtExport.Columns.Add("Qty", Type.GetType("System.Int32"));
  385. dtExport.Columns.Add("Finished Weight(ct)", Type.GetType("System.Decimal"));
  386. dtExport.Columns.Add("Rough Weight(ct)", Type.GetType("System.Decimal"));
  387. dtExport.Columns.Add("Price(USD)", Type.GetType("System.Decimal"));
  388. dtExport.Columns.Add("Shape", Type.GetType("System.String"));
  389. dtExport.Columns.Add("CertificateNo", Type.GetType("System.String"));
  390. dtExport.Columns.Add("Color", Type.GetType("System.String"));
  391. dtExport.Columns.Add("Clarity", Type.GetType("System.String"));
  392. dtExport.Columns.Add("Polish", Type.GetType("System.String"));
  393. int number = 1;
  394. foreach (DataRow row in dt.Rows)
  395. {
  396. DataRow r = dtExport.NewRow();
  397. r["No."] = number;
  398. r["Product No."] = row["ProductSN"].ToString();
  399. r["Measurement"] = row["Measurement"].ToString();
  400. r["Sort"] = row["Sort"].ToString();
  401. r["Location"] = row["Location"].ToString();
  402. r["Qty"] = row["Qty"].ToString();
  403. r["Finished Weight(ct)"] = string.IsNullOrEmpty(row["FinishedWeight"].ToString()) ? "0" : row["FinishedWeight"].ToString();
  404. r["Rough Weight(ct)"] = string.IsNullOrEmpty(row["RoughWeight"].ToString()) ? "0" : row["RoughWeight"].ToString();
  405. r["Price(USD)"] = string.IsNullOrEmpty(row["Price"].ToString()) ? 0 : decimal.Parse(row["Price"].ToString());
  406. r["Shape"] = row["Shape"].ToString();
  407. r["CertificateNo"] = row["CertificateNo"].ToString();
  408. r["Color"] = row["Color"].ToString();
  409. r["Clarity"] = row["Clarity"].ToString();
  410. r["Polish"] = row["Polish"].ToString();
  411. dtExport.Rows.Add(r);
  412. number++;
  413. }
  414. gv.DataSource = dtExport;
  415. gv.DataBind();
  416. Export2(gv);
  417. }
  418. }
  419. /// <summary>
  420. /// Replace any of the contained controls with literals
  421. /// </summary>
  422. /// <param name="control"></param>
  423. private static void PrepareControlForExport(Control control)
  424. {
  425. for (int i = 0; i < control.Controls.Count; i++)
  426. {
  427. Control current = control.Controls[i];
  428. if (current is LinkButton)
  429. {
  430. control.Controls.Remove(current);
  431. control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
  432. }
  433. else if (current is ImageButton)
  434. {
  435. control.Controls.Remove(current);
  436. control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
  437. }
  438. else if (current is HyperLink)
  439. {
  440. control.Controls.Remove(current);
  441. control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
  442. }
  443. else if (current is DropDownList)
  444. {
  445. control.Controls.Remove(current);
  446. control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
  447. }
  448. else if (current is CheckBox)
  449. {
  450. control.Controls.Remove(current);
  451. control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
  452. }
  453. if (current.HasControls())
  454. {
  455. GridViewExportUtil.PrepareControlForExport(current);
  456. }
  457. }
  458. }
  459. }
  460. public class AsposeCell
  461. {
  462. public static void Export(DataTable dt)
  463. {
  464. var name = DateTime.Now.ToString("yyyyMMddhhmmss") + new Random(DateTime.Now.Second).Next(10000) + ".xls";
  465. MemoryStream ms = OutFileToStream(dt);
  466. HttpContext.Current.Response.Clear();
  467. HttpContext.Current.Response.ContentType = "application/ms-excel";
  468. HttpContext.Current.Response.AddHeader("Content-Type", "application/octet-stream");
  469. HttpContext.Current.Response.Charset = "utf-8";
  470. HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(name, System.Text.Encoding.UTF8));
  471. HttpContext.Current.Response.AddHeader("Content-Length", ms.ToArray().LongLength.ToString());
  472. HttpContext.Current.Response.BinaryWrite(ms.ToArray());
  473. HttpContext.Current.Response.Flush();
  474. HttpContext.Current.Response.Clear();
  475. HttpContext.Current.Response.End();
  476. }
  477. public static MemoryStream OutFileToStream(DataTable dt)
  478. {
  479. Workbook workbook = new Workbook(); //工作簿
  480. workbook.Worksheets.Clear();
  481. for (int j = 0; j < System.Math.Ceiling((dt.Rows.Count / (double)65534)); j++)
  482. {
  483. workbook.Worksheets.Add("" + j);
  484. Worksheet sheet = workbook.Worksheets[j];
  485. Cells cells = sheet.Cells;//单元格
  486. //为标题设置样式
  487. Aspose.Cells.Style styleTitle = workbook.Styles[workbook.Styles.Add()];//新增样式
  488. styleTitle.HorizontalAlignment = TextAlignmentType.Center;//文字居中
  489. styleTitle.Font.Name = "宋体";//文字字体
  490. styleTitle.Font.Size = 18;//文字大小
  491. styleTitle.Font.IsBold = true;//粗体
  492. //样式2
  493. Aspose.Cells.Style style2 = workbook.Styles[workbook.Styles.Add()];//新增样式
  494. style2.HorizontalAlignment = TextAlignmentType.Center;//文字居中
  495. style2.Font.Name = "宋体";//文字字体
  496. style2.Font.Size = 14;//文字大小
  497. style2.Font.IsBold = true;//粗体
  498. style2.IsTextWrapped = false;//单元格内容自动换行
  499. //样式3
  500. Aspose.Cells.Style style3 = workbook.Styles[workbook.Styles.Add()];//新增样式
  501. style3.HorizontalAlignment = TextAlignmentType.Center;//文字居中
  502. style3.Font.Name = "宋体";//文字字体
  503. style3.Font.Size = 12;//文字大小
  504. style2.IsTextWrapped = false;//单元格内容自动换行
  505. int Colnum = dt.Columns.Count;//表格列数
  506. int Rownum = dt.Rows.Count;//表格行数
  507. //生成行2 列名行
  508. for (int i = 0; i < Colnum; i++)
  509. {
  510. cells[0, i].PutValue(dt.Columns[i].ColumnName);
  511. cells[0, i].SetStyle(style2);
  512. cells.SetRowHeight(0, 25);
  513. for (int col = 0; col < Colnum; col++)
  514. {
  515. sheet.AutoFitColumn(col, 0, Rownum);
  516. }
  517. for (int col = 0; col < Colnum; col++)
  518. {
  519. cells.SetColumnWidthPixel(col, cells.GetColumnWidthPixel(col) + 30);
  520. }
  521. }
  522. //生成数据行
  523. for (int i = 0; i < 65534; i++)
  524. {
  525. if ((j * 65534 + i) >= Rownum)
  526. {
  527. break;
  528. }
  529. for (int k = 0; k < Colnum; k++)
  530. {
  531. cells[1 + i, k].PutValue(dt.Rows[j * 65534 + i][k].ToString());
  532. cells[1 + i, k].SetStyle(style3);
  533. }
  534. cells.SetRowHeight(1 + i, 24);
  535. }
  536. }
  537. MemoryStream ms = workbook.SaveToStream();
  538. return ms;
  539. }
  540. }
  541. }