ASP.NET DataSet資料生成Excel文件匯出下載
阿新 • • 發佈:2019-02-01
public partial class am_ElectronicInvoinceExportExcel : BasePage { protected void Page_Load(object sender, EventArgs e) { ExcelFile excelFile = new ExcelFile(); excelFile.Worksheets.Add("發票資訊表"); ExcelWorksheet xlsheet = excelFile.Worksheets[0]; if (excelFile == null || xlsheet == null) { base.SetException("無法建立Excel物件,可能您的計算機未安裝Excel。請先安裝Excel"); return; } DataSet ds = new DataSet(); ds.Merge((DataSet)Session[WebConst.DOWNLOADDATA]); //Session[WebConst.DOWNLOADDATA] = null; if (OperateUI.HaveData(ds)) { #region 列名 int rowIndex = 0; xlsheet.Rows[rowIndex].Height = 400; xlsheet.Rows[rowIndex].Style.Font.Name = "宋體"; xlsheet.Rows[rowIndex].Style.Font.Size = 9 * 20; xlsheet.Rows[rowIndex].Style.VerticalAlignment = VerticalAlignmentStyle.Center; xlsheet.Rows[rowIndex].Style.HorizontalAlignment = HorizontalAlignmentStyle.Center; CellRange cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 0, rowIndex, 0); cellRange.Merged = true; cellRange.Value = "發票程式碼"; cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 1, rowIndex, 1); cellRange.Merged = true; cellRange.Value = "發票號碼"; cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 2, rowIndex, 2); cellRange.Merged = true; cellRange.Value = "提單號"; cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 3, rowIndex, 3); cellRange.Merged = true; cellRange.Value = "進出口"; cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 4, rowIndex, 4); cellRange.Merged = true; cellRange.Value = "幣別"; cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 5, rowIndex, 5); cellRange.Merged = true; cellRange.Value = "金額"; cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 6, rowIndex, 6); cellRange.Merged = true; cellRange.Value = "人民幣金額"; cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 7, rowIndex, 7); cellRange.Merged = true; cellRange.Value = "開票日期"; #endregion #region 明細資料 foreach (DataRow dr in ds.Tables[0].Rows) { rowIndex++; xlsheet.Rows[rowIndex].Height = 400; xlsheet.Rows[rowIndex].Style.Font.Name = "宋體"; xlsheet.Rows[rowIndex].Style.Font.Size = 9 * 20; xlsheet.Rows[rowIndex].Style.VerticalAlignment = VerticalAlignmentStyle.Center; xlsheet.Rows[rowIndex].Style.HorizontalAlignment = HorizontalAlignmentStyle.Center; cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 0, rowIndex, 0); cellRange.Merged = true; cellRange.Value = dr["ARIVCD"].ToString().Trim(); cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 1, rowIndex, 1); cellRange.Merged = true; cellRange.Value = dr["ARIVNO"].ToString().Trim(); cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 2, rowIndex, 2); cellRange.Merged = true; cellRange.Value = dr["INBLNO"].ToString().Trim(); cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 3, rowIndex, 3); cellRange.Merged = true; cellRange.Value = dr["IHIE"].ToString().Trim(); cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 4, rowIndex, 4); cellRange.Merged = true; cellRange.Value = dr["INCURR"].ToString().Trim(); cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 5, rowIndex, 5); cellRange.Merged = true; cellRange.Value = dr["INAMT"].ToString().Trim(); cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 6, rowIndex, 6); cellRange.Merged = true; cellRange.Value = dr["INCNY"].ToString().Trim(); cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 7, rowIndex, 7); cellRange.Merged = true; cellRange.Value = dr["INDATE"].ToString().Trim(); } #endregion #region 總額統計 rowIndex++; xlsheet.Rows[rowIndex].Height = 400; xlsheet.Rows[rowIndex].Style.Font.Name = "宋體"; xlsheet.Rows[rowIndex].Style.Font.Size = 9 * 20; xlsheet.Rows[rowIndex].Style.VerticalAlignment = VerticalAlignmentStyle.Center; xlsheet.Rows[rowIndex].Style.HorizontalAlignment = HorizontalAlignmentStyle.Center; decimal ipamt = 0; decimal ipcny = 0; try { ipamt = Decimal.Parse(ds.Tables[0].Compute("SUM(INAMT)", "").ToString()); } catch { } try { ipcny = Decimal.Parse(ds.Tables[0].Compute("SUM(INCNY)", "").ToString()); } catch { } cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 5, rowIndex, 5); cellRange.Merged = true; cellRange.Value = string.Format("{0:N2}", ipamt); cellRange = xlsheet.Cells.GetSubrangeAbsolute(rowIndex, 6, rowIndex, 6); cellRange.Merged = true; cellRange.Value = string.Format("{0:N2}", ipcny); #endregion #region 檔案匯出 string strFileName = Path.Combine(Request.MapPath(".") + "\\report\\", DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls"); excelFile.SaveXls(strFileName); Response.ClearContent(); Response.ClearHeaders(); Response.BufferOutput = true; Response.ContentType = "application/vnd.ms-excel"; Response.AddHeader("Content-Disposition", "attachment; filename=" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".xls"); Response.WriteFile(strFileName); Response.Flush(); try { System.IO.File.Delete(strFileName); } catch { } #endregion } else { base.SetException("瀏覽器快取資料出現異常,請重啟瀏覽器後重新操作"); return; } } }