前臺:
<asp:Button ID="btnMoney" runat="server" Text="經費使用總結表" CssClass="admin_cx marginleft" Height="25" OnClick="btnMoney_Click" />
后臺:
protected void btnMoney_Click(object sender, EventArgs e){if (this.ArticleId.Value == ""){this.Alert("請點擊下拉選擇您想操作的文章");return;}int id = Convert.ToInt32(this.ArticleId.Value);string sqlwhere = " ArticleId = " + id + " and State=" + (int)CedureEnum.CapitalDeclareState.Complete;DataSet ds = IBLL.Factory.infoCapitalDeclareCreate().GetPageList(sqlwhere, ref oPageInfo, "a.CreateDate desc");DataTable dt = ds.Tables[0];LeadExcel(dt, "匯總表", "three", id);}#region 生成xls文件//按采購人統計protected void LeadExcel(DataTable dt, string opetes, string state, int ArticleId=0){string filename = opetes + DateTime.Now.ToString("yyyyMMdd") + ".xls";MemoryStream ms = null;try{ms = new MemoryStream();// IWorkbook xssfworkbook = null;if (filename.IndexOf(".xlsx") > -1){xssfworkbook = new XSSFWorkbook();}elsexssfworkbook = new HSSFWorkbook();ISheet sheet = xssfworkbook.CreateSheet("Sheet1");ICellStyle cellstyle = xssfworkbook.CreateCellStyle();//設置垂直居中格式cellstyle.Alignment = HorizontalAlignment.CENTER;SetCellRangeAddress(sheet, 0, 0, 0, 7);sheet.DefaultColumnWidth = 28;//string title = "車輛信息";//startdate.ToString("yyyy-MM-dd") + "至" + enddate.ToString("yyyy-MM-dd") + "";//sheet.CreateRow(0).CreateCell(0).SetCellValue("車輛信息");//sheet.GetRow(0).GetCell(0).CellStyle = cellstyle;//SetCellRangeAddress(sheet, 0, 0, 8, 11);//sheet.CreateRow(0).CreateCell(8).SetCellValue("審核結果");//sheet.GetRow(0).GetCell(8).CellStyle = cellstyle;sheet.CreateRow(1).CreateCell(0).SetCellValue("公司名稱");sheet.GetRow(1).CreateCell(1).SetCellValue("項目名稱");sheet.GetRow(1).CreateCell(2).SetCellValue("申請人");sheet.GetRow(1).CreateCell(3).SetCellValue("聯系人");sheet.GetRow(1).CreateCell(4).SetCellValue("聯系電話");sheet.GetRow(1).CreateCell(5).SetCellValue("填報日期"); ;if (dt != null && dt.Rows.Count > 0){int row = 2;//行int endrow = 0;//結束行for (int i = 0; i < dt.Rows.Count; i++){sheet.CreateRow(row).CreateCell(0).SetCellValue(dt.Rows[i]["Company"] + "");sheet.GetRow(row).CreateCell(1).SetCellValue(dt.Rows[i]["ProjectName"] + "");sheet.GetRow(row).CreateCell(2).SetCellValue(dt.Rows[i]["Proposer"] + "");sheet.GetRow(row).CreateCell(3).SetCellValue(dt.Rows[i]["Contacts"] + "");sheet.GetRow(row).CreateCell(4).SetCellValue(dt.Rows[i]["Mobile"] + "");sheet.GetRow(row).CreateCell(5).SetCellValue(dt.Rows[i]["CreateDate"] + "");row++;}}xssfworkbook.Write(ms);Response.Clear();Response.Charset = "utf-8";Response.ContentEncoding = System.Text.Encoding.UTF8;Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(filename));Response.ContentType = "application/octet-stream";Response.BinaryWrite(ms.ToArray());Response.End();// }catch (Exception ex){throw ex;}finally{ms.Dispose();}}/// <summary>/// 合并單元格/// </summary>/// <param name="sheet">要合并單元格所在的sheet</param>/// <param name="rowstart">開始行的索引</param>/// <param name="rowend">結束行的索引</param>/// <param name="colstart">開始列的索引</param>/// <param name="colend">結束列的索引</param>public static void SetCellRangeAddress(ISheet sheet, int rowstart, int rowend, int colstart, int colend){CellRangeAddress cellRangeAddress = new CellRangeAddress(rowstart, rowend, colstart, colend);sheet.AddMergedRegion(cellRangeAddress);}#endregion
?