c# 導出excel格式xlsx



         string sb="";//sql字符串
         AttachmentConfigSection configSection = ConfigurationManager.GetSection("AttachmentConfig") as AttachmentConfigSection; string path = ""; string sheetName = "Sheet1"; string fileName = "月結狀態處理.xlsx"; if (!string.IsNullOrEmpty(configSection.ElementInformation.Source)) { path = configSection.TargetPath.Path; } else { errormsg = "模版不正確"; } WriteLog("xiujiamontyle 123....."+sb.ToString()); string index = "\\Upload\\XiuJiaMontly\\" + fileName; string descFile = path + index.ToString(); FileInfo fileinfo = new FileInfo(descFile); ExcelPackage excel = new ExcelPackage(fileinfo); ExcelWorksheet sheet = excel.Workbook.Worksheets[sheetName]; DataTable dt = DBHelperExtend.Query(sb.ToString()).Tables[0]; int row = dt.Rows.Count; //行 sheet.Cells[1, 1].Value = "任務編號"; sheet.Cells[1, 2].Value = "月結狀態"; sheet.Cells[1, 3].Value = "提交人"; sheet.Cells[1, 4].Value ="員工編號"; sheet.Cells[1, 5].Value ="部門"; sheet.Cells[1, 6].Value ="區域"; sheet.Cells[1, 7].Value ="休假類別"; sheet.Cells[1, 8].Value ="是否住院"; sheet.Cells[1, 9].Value ="原因"; sheet.Cells[1, 10].Value ="休假時間"; sheet.Cells[1, 11].Value = "天數時數"; sheet.Cells[1, 12].Value = "提交日期"; sheet.Cells[1, 13].Value = "狀態"; sheet.Cells[1, 14].Value = "撤回編號"; sheet.Cells[1, 15].Value = "撤回日期"; sheet.Cells[1, 16].Value = "撤回狀態"; sheet.Cells[1, 17].Value = "用年假抵扣"; for (int i = 0; i < dt.Rows.Count; i++) { sheet.Cells[i + 2, 1].Value = dt.Rows[i]["TaskID"]; sheet.Cells[i + 2, 2].Value = dt.Rows[i]["monthlyStatus"]; sheet.Cells[i + 2, 3].Value = dt.Rows[i]["RequestAccount"]; sheet.Cells[i + 2, 4].Value = dt.Rows[i]["hrid"]; sheet.Cells[i + 2, 5].Value = dt.Rows[i]["RequestDept"]; sheet.Cells[i + 2, 6].Value = dt.Rows[i]["RequestLocation"]; sheet.Cells[i + 2, 7].Value = dt.Rows[i]["XiuJiaType"]; sheet.Cells[i + 2, 8].Value = dt.Rows[i]["IsInHospital"]; sheet.Cells[i + 2, 9].Value = dt.Rows[i]["Description"]; sheet.Cells[i + 2, 10].Value = dt.Rows[i]["XiuJiaDate"]; sheet.Cells[i + 2, 11].Value = dt.Rows[i]["TotalTime"]; sheet.Cells[i + 2, 12].Value = dt.Rows[i]["SubmitTime"]; sheet.Cells[i + 2, 13].Value = dt.Rows[i]["Status"]; sheet.Cells[i + 2, 14].Value = dt.Rows[i]["CH_TaskID"]; sheet.Cells[i + 2, 15].Value = dt.Rows[i]["cheHuiTime"]; sheet.Cells[i + 2, 16].Value = dt.Rows[i]["cheHuiStatus"]; sheet.Cells[i + 2, 17].Value = dt.Rows[i]["AnnualDeductible"]; } byte[] b = excel.GetAsByteArray(); HttpContext curContext = System.Web.HttpContext.Current; // curContext.Response.ContentType = "application/vnd.ms-excel"; curContext.Response.ContentType = "text/plain";//Content-Disposition // curContext.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; curContext.Response.AddHeader("content-disposition", "attachment; filename=XiuJiaMonthlyHandel.xlsx"); curContext.Response.Charset = "gb2312"; //必須寫,否則會有亂碼 curContext.Response.ContentEncoding = System.Text.Encoding.UTF7; //必須寫,否則會有亂碼 curContext.Response.AddHeader("Content-Length", b.Length.ToString()); curContext.Response.BinaryWrite(b); curContext.Response.End();

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM