1 //导出Excel
2 function exportExcel() {
3 type = $("#typeid").val();
4 var url = "../../../Handler/Inspect/ReportManage/DXTodayHandler.ashx?action=export&type=" + type;
5 window.open(url);
6 }
1 ///
2 /// 导出
3 ///
4 /// 导出的表
5 public void Export(DataTable dt)
6 {
7 Export export = new Export();
8 HttpContext context = System.Web.HttpContext.Current;
9 string createdate = string.Empty;
10 createdate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
11 createdate = createdate.Replace(" ", "_");
12 context.Response.ContentType = "text/html";
13 context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
14 string filename = "今日报废导出(编号)" + createdate + ".xls";
15 filename = HttpUtility.UrlEncode(filename, System.Text.Encoding.GetEncoding("UTF-8"));
16 context.Response.ContentType = "application/vnd.ms-excel";
17 context.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", filename));
18 context.Response.Clear();
19 export.InitializeWorkbook();
20 export.GenerateReport(dt);
21 context.Response.BinaryWrite(export.WriteToStream().GetBuffer());
22 context.Response.End();
23 }
1 ///
2 /// 作者:fxw
3 /// 今日报废
4 ///
5 ///
6 public void GenerateReport(DataTable dt)
7 {
8 ISheet sheet1 = hssfworkbook.CreateSheet("DXIDToday");
9
10 #region 各列宽度
11 sheet1.SetColumnWidth(0, 5000);
12 for (int i = 0; i < dt.Columns.Count; i++)
13 {
14 sheet1.SetColumnWidth(i + 1, 5000);
15 }
16 #endregion
17
18 #region 标题样式
19 //新建一个字体样式对象
20 IFont fontTitle = hssfworkbook.CreateFont();
21 fontTitle.FontHeightInPoints = 15;
22 //设置字体加粗样式
23 fontTitle.Boldweight = (short)FontBoldWeight.Bold;
24 ICellStyle styleTiltle = hssfworkbook.CreateCellStyle();
25 //设置单元格的样式:水平对齐居中
26 styleTiltle.Alignment = HorizontalAlignment.Center;
27 styleTiltle.SetFont(fontTitle);
28 //设置单元格的样式:上下对齐居中
29 styleTiltle.VerticalAlignment = VerticalAlignment.Center;
30 #endregion
31
32 #region 子表头样式
33 IFont fontSmallTitle = hssfworkbook.CreateFont();
34 fontSmallTitle.FontHeightInPoints = 10;
35 fontSmallTitle.Boldweight = (short)FontBoldWeight.Bold;
36 ICellStyle styleSmallTitle = hssfworkbook.CreateCellStyle();
37 styleSmallTitle.Alignment = HorizontalAlignment.Center;
38 styleSmallTitle.SetFont(fontSmallTitle);
39 styleSmallTitle.VerticalAlignment = VerticalAlignment.Center;
40 #endregion
41
42 #region 内容基本样式
43 IFont fontContent = hssfworkbook.CreateFont();
44 fontContent.FontHeightInPoints = 10;
45 ICellStyle styleContent = hssfworkbook.CreateCellStyle();
46 styleContent.Alignment = HorizontalAlignment.Center;
47 styleContent.SetFont(fontContent);
48 styleContent.VerticalAlignment = VerticalAlignment.Center;
49 styleContent.BorderBottom = BorderStyle.Thin;
50 styleContent.BorderLeft = BorderStyle.Thin;
51 styleContent.BorderRight = BorderStyle.Thin;
52 styleContent.BorderTop = BorderStyle.Thin;
53 #endregion
54
55 #region 底部样式
56 IFont fontFooter = hssfworkbook.CreateFont();
57 fontFooter.FontHeightInPoints = 10;
58 fontFooter.Boldweight = (short)FontBoldWeight.Bold;
59 ICellStyle styleFooter = hssfworkbook.CreateCellStyle();
60 styleFooter.Alignment = HorizontalAlignment.Center;
61 styleFooter.SetFont(fontFooter);
62 styleFooter.VerticalAlignment = VerticalAlignment.Center;
63 styleFooter.BorderBottom = BorderStyle.Thin;
64 styleFooter.BorderLeft = BorderStyle.Thin;
65 styleFooter.BorderRight = BorderStyle.Thin;
66 styleFooter.BorderTop = BorderStyle.Thin;
67 #endregion
68
69
70
71 //第一行
72 IRow row0 = sheet1.CreateRow(0);
73 row0.HeightInPoints = 30;
74 row0.CreateCell(0).SetCellValue("序号");
75 row0.GetCell(0).CellStyle = styleTiltle;
76 row0.CreateCell(1).SetCellValue("不良编号");
77 row0.CreateCell(2).SetCellValue("不良项目名");
78 row0.CreateCell(3).SetCellValue("当前报废数");
79 row0.GetCell(1).CellStyle = styleTiltle;
80 row0.GetCell(2).CellStyle = styleTiltle;
81 row0.GetCell(3).CellStyle = styleTiltle;
82 for (int i = 3; i < dt.Columns.Count; i++)
83 {
84 row0.CreateCell(i + 1).SetCellValue(dt.Columns[i].ColumnName.ToString());
85 row0.GetCell(i + 1).CellStyle = styleTiltle;
86 }
87 // 第二行开始填充数据
88
89 int rowNumRe = 1;
90 if (dt != null && dt.Rows.Count > 0)
91 {
92 int li = dt.Rows.Count;
93 for (int i = 0; i < li; i++)
94 {
95 IRow rowInfo = sheet1.CreateRow(rowNumRe);
96 rowInfo.HeightInPoints = 22;
97 rowInfo.CreateCell(0).SetCellValue(i + 1);
98 rowInfo.GetCell(0).CellStyle = styleContent;
99 for (int c = 0; c < dt.Columns.Count; c++)
100 {
101 rowInfo.CreateCell(c + 1).SetCellValue(dt.Rows[i][dt.Columns[c].ColumnName.ToString()].ToString());
102 rowInfo.GetCell(c + 1).CellStyle = styleContent;
103 }
104 rowNumRe++;
105 }
106 }
107 }