此方法是试用epplus
OfficeOpenXml.ExcelPackage ep = new OfficeOpenXml.ExcelPackage();
OfficeOpenXml.ExcelWorkbook wb = ep.Workbook;
OfficeOpenXml.ExcelWorksheet ws = wb.Worksheets.Add("我的工作表");
//配置文件属性
wb.Properties.Category = "类别";
wb.Properties.Author = "作者";
wb.Properties.Comments = "备注";
wb.Properties.Company = "公司";
wb.Properties.Keywords = "关键字";
wb.Properties.Manager = "管理者";
wb.Properties.Status = "内容状态";
wb.Properties.Subject = "主题";
wb.Properties.Title = "标题";
wb.Properties.LastModifiedBy = "最后一次保存者";
//写数据
ws.Cells[1, 1].Value = "Hello";
ws.Cells[1, 1].Style.Numberformat.Format = "yyyy-MM-dd";
ws.Column(1).Width = 40;//修改列宽
ws.Cells["B1"].Value = "World";
ws.Cells[3, 3, 3, 5].Merge = true;
ws.Cells[3, 3].Value = "Cells[3, 3, 3, 5]合并";
ws.Cells["A4:D5"].Merge = true;
ws.Cells["A4:D5"].Style.HorizontalAlignment = OfficeOpenXml.Style.ExcelHorizontalAlignment.Center;//居中
ws.Cells["A4"].Value = "Cells["A4:D5"]合并";
//写到客户端(下载)
context.Response.Clear();
context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
context.Response.AddHeader("content-disposition", "attachment; filename=FileFlow.xls");
context.Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
context.Response.BinaryWrite(ep.GetAsByteArray());
//ep.SaveAs(Response.OutputStream); 第二种方式
context.Response.Flush();
context.Response.End();
此方法是试用 npoi
//HSSFWorkbook hssfworkbook = new HSSFWorkbook();
//DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
//dsi.Company = "blog.csdn.net";
//hssfworkbook.DocumentSummaryInformation = dsi;
//SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
//si.Subject = "Export Excel";
//hssfworkbook.SummaryInformation = si;
//ISheet sheet1 = hssfworkbook.CreateSheet("Sheet1");
//IRow row0 = sheet1.CreateRow(0);
//row0.CreateCell(0).SetCellValue("姓名");
//row0.CreateCell(1).SetCellValue("年龄");
//row0.CreateCell(2).SetCellValue("性别");
//IRow row1 = sheet1.CreateRow(1);
//row1.CreateCell(0).SetCellValue("张三");
//row1.CreateCell(1).SetCellValue(29);
//row1.CreateCell(2).SetCellValue("男");
//IRow row2 = sheet1.CreateRow(2);
//row2.CreateCell(0).SetCellValue("李四");
//row2.CreateCell(1).SetCellValue(35);
//row2.CreateCell(2).SetCellValue("男");
//IRow row3 = sheet1.CreateRow(3);
//row3.CreateCell(0).SetCellValue("王五");
//row3.CreateCell(1).SetCellValue(20);
//row3.CreateCell(2).SetCellValue("女");
//MemoryStream ms = new MemoryStream();
//hssfworkbook.Write(ms);
////如果文件名是中文的话,需要进行编码转换,否则浏览器看到的下载文件是乱码。
//string fileName = HttpUtility.UrlEncode("Excel.xls");
//context.Response.ContentType = "application/vnd.ms-excel";
////Response.ContentType = "application/download"; //也可以设置成download
//context.Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", fileName));
//context.Response.Buffer = true;
//context.Response.Clear();
//context.Response.BinaryWrite(ms.GetBuffer());
//context.Response.End();