public async Task <BaseResponse> Xlsx() { List <ExcelTestClass> list = ExcelTestClass.GetList(); //BaseDirectory后面有\所以exel前面就不加\了 var storePath = AppDomain.CurrentDomain.BaseDirectory + "Excel"; if (!Directory.Exists(storePath)) { Directory.CreateDirectory(storePath); } string fileNam = "导出数据-" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx"; string fullPath = storePath + "\\" + fileNam; XSSFWorkbook workbook = new XSSFWorkbook(); POIXMLProperties props = workbook.GetProperties(); props.CoreProperties.Creator = "北京易天正诚信息技术有限公司"; props.CoreProperties.Subject = "壁虎科技"; props.CoreProperties.Title = "壁虎科技"; props.CoreProperties.Created = DateTime.Now; props.CustomProperties.AddProperty("壁虎科技", "壁虎科技"); XSSFCellStyle headStyle = (XSSFCellStyle)workbook.CreateCellStyle(); headStyle.Alignment = HorizontalAlignment.Center; XSSFFont font = (XSSFFont)workbook.CreateFont(); font.FontHeightInPoints = 12; font.Boldweight = 700; headStyle.SetFont(font); var sheet = workbook.CreateSheet(); IRow headerRow = sheet.CreateRow(0); var headCell = headerRow.CreateCell(0); headCell.SetCellValue("111"); FileStream file = new FileStream(fullPath, FileMode.Create); workbook.Write(file); file.Close(); return(BaseResponse.Ok(fullPath)); }
/// <summary> /// /// </summary> /// <returns></returns> public async Task <byte[]> ListToExcelByte() { List <ExcelTestClass> list = ExcelTestClass.GetList(); string fileNam = "导出数据-" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls"; //HSSFWorkbook hssfworkbook=await GetWorkbook(list,"导出通话记录"); HSSFWorkbook hssfworkbook = ListToExcelExtention.ListWorkbookExchange(list, "导出通话记录"); using (MemoryStream ms = new MemoryStream()) { hssfworkbook.Write(ms); await ms.FlushAsync(); ms.Position = 0; hssfworkbook.Close(); byte[] buf = ms.ToArray(); return(buf); } }
public async Task <BaseResponse> ListToExcelFileXlsx() { List <ExcelTestClass> list = ExcelTestClass.GetList(); //BaseDirectory后面有\所以exel前面就不加\了 var storePath = AppDomain.CurrentDomain.BaseDirectory + "Excel"; if (!Directory.Exists(storePath)) { Directory.CreateDirectory(storePath); } string fileNam = "导出数据-" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx"; string fullPath = storePath + "\\" + fileNam; //HSSFWorkbook hssfworkbook=await GetWorkbook(list,"导出通话记录"); XSSFWorkbook hssfworkbook = ListToExcelExtention.ListToXlsx(list, "导出通话记录"); //把这个HSSFWorkbook实例写入文件 FileStream file = new FileStream(fullPath, FileMode.Create); hssfworkbook.Write(file); file.Close(); return(BaseResponse.Ok(fullPath)); }