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();

  

猜你喜欢

转载自www.cnblogs.com/yachao1120/p/9497935.html