1 回答

TA貢獻(xiàn)4條經(jīng)驗(yàn) 獲得超0個(gè)贊
首先引用 NPOI
代碼語言c# ??
也不知道樓主用的什么語言 ?希望能對(duì)樓主有幫助 ??
我不是大神 ?只是剛好照著例子做過導(dǎo)出excel ?說的不完善 和有錯(cuò)誤的地方要告訴我哦,不要讓我再繼續(xù)錯(cuò)下去了哦
?public FileResult DaochuExcel()
? ? ? ? {
? ? ? ? ? ? //創(chuàng)建Excel文件的對(duì)象
? ? ? ? ? ? NPOI.HSSF.UserModel.HSSFWorkbook book = new NPOI.HSSF.UserModel.HSSFWorkbook();
? ? ? ? ? ? //添加一個(gè)sheet
? ? ? ? ? ? NPOI.SS.UserModel.ISheet sheet1 = book.CreateSheet("Sheet1");
? ?//獲取list數(shù)據(jù)
? ? ?? ?IQueryable<AliExpressOrderForm> qs = null;
? ? ? ?(這一段是查詢數(shù)據(jù))
? ? ? ? ? ? List<AliExpressOrderForm> ListInfo = qs.ToList(); ?
? ? ? ? ? ? NPOI.SS.UserModel.IRow row1 = sheet1.CreateRow(0);
? ? ? ? ? ? row1.Height = 3 * 265;
? ? ? ? ?? IFont cfont = book.CreateFont(); ?//自己設(shè)置的一個(gè)字體樣式
? ? ? ? ? ? ?cfont.FontName = "宋體";
? ? ? ? ? ? ?cfont.FontHeight = 1 * 256;
? ? ?row1.CreateCell(0).SetCellValue("訂單號(hào)"); ? //自己設(shè)置的標(biāo)題
? ? ? ? ??? row1.CreateCell(1).SetCellValue("訂單狀態(tài)");
? ? ? ? ? ?
? ? ? ? ? ? sheet1.SetColumnWidth(0, 20*256); ?//設(shè)置列寬
? ? ? ? ? ? sheet1.SetColumnWidth(1, 15*256);
? ? ? ? ?
? ? ? ? ? ? for (int i = 0; i < ListInfo.Count; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? NPOI.SS.UserModel.IRow rowtemp = sheet1.CreateRow(i + 1);
? ? ? ? ? ? ? ? rowtemp.Height = 3*265;
? ? ? ? ? ? ??
? ? ? ? ? ? ? ? rowtemp.CreateCell(0).SetCellValue(ListInfo[i].order_number);
? ? ? ? ? ? ? ?
? ? ? ? ? ? ? ? ? ? rowtemp.Cells[0].CellStyle.Alignment = HorizontalAlignment.Center;
? ? ? ? ? ? ? ? ? ? rowtemp.Cells[0].CellStyle.VerticalAlignment = VerticalAlignment.Center;//這兩句是垂直居中和水平居中
? ? ? ? ? ? ? ? ? ? rowtemp.Cells[0].CellStyle.WrapText = true; //自動(dòng)換行
? ? ? ? ? ? ? ? ?? ?rowtemp.Cells[0].CellStyle.GetFont(book).FontName = "宋體";
? ? ? ??? ? rowtemp.Cells[0].CellStyle.GetFont(book).FontHeight = 1 * 256;
? ? ? ???rowtemp.CreateCell(1).SetCellValue(ListInfo[i].status); ?//把值寫到對(duì)應(yīng)的格子里
? ? ? ? ? ? ? ? rowtemp.CreateCell(2).SetCellValue(ListInfo[i].salesman);
? ? ? ? ? ??
? }
? ? ? ? ? ? Response.ContentType = "application/vnd.ms-excel;charset=UTF-8";
? ? ? ? ? ? // 寫入到客戶端?
? ? ? ? ? ? System.IO.MemoryStream ms = new System.IO.MemoryStream();
? ? ? ? ? ? book.Write(ms);
? ? ? ? ? ? ms.Seek(0, SeekOrigin.Begin);
? ? ? ? ? ? ms.Flush();
? ? ? ? ? ? ms.Position = 0;
? ? ? ? ? ? return File(ms, "application/vnd.ms-excel", "文件名.xls");
? ? ? ? }
添加回答
舉報(bào)