npoi把xlsx文件转为html,使用NPOI完成导出Excel文件

1 [HttpPost]//只接受post请求2 public voidPrintExcel()3 {4 //接收参数 int id=Int32.Parse(Requert.Form["id"]);

5 string trainShift = Session["trainshiftNum"].ToString();6 #region 转换成DataSet

7 var query = from s indb.Students8 where s.IsDelete == false && s.TrainShiftNum ==trainShift9 select newStu10 {11 StudentDepartment =s.StudentDepartment,12 StudentGender =s.StudentGender,13 StuDentIdCard =s.StuDentIdCard,14 StudentName =s.StudentName,15 StudentNation =s.StudentNation,16 StudentPosition =s.StudentPosition,17 StudentUnit =s.StudentUnit,18 TrainShiftNum =s.TrainShiftNum,19 Remark =s.Remark20 };21 DataTable dt = query.ToDataTable(rec => new object[] { query });//调用转换DataTable方法22 #endregion

23

24 HSSFWorkbook book = newHSSFWorkbook();25 MemoryStream ms = newMemoryStream();26 ISheet sheet = book.CreateSheet("sheet1");27

28 //首列

29 NPOI.SS.UserModel.IRow row = sheet.CreateRow(0);30 row.CreateCell(0).SetCellValue("序号");31 row.CreateCell(1).SetCellValue("所在单位");32 row.CreateCell(2).SetCellValue("所在部门");33 row.CreateCell(3).SetCellValue("姓名");34 row.CreateCell(4).SetCellValue("职务");35 row.CreateCell(5).SetCellValue("性别");36 row.CreateCell(6).SetCellValue("民族");37 row.CreateCell(7).SetCellValue("出生年月");38 row.CreateCell(8).SetCellValue("组");39 row.CreateCell(9).SetCellValue("备注");40 #region

41 第一列

42 //NPOI.SS.UserModel.IRow row1 = sheet.CreateRow(1);43 //row1.CreateCell(0).SetCellValue("所在单位");

44

45 第二列

46 //NPOI.SS.UserModel.IRow row2 = sheet.CreateRow(2);47 //row2.CreateCell(0).SetCellValue("所在部门");

48

49 第三列

50 //NPOI.SS.UserModel.IRow row3 = sheet.CreateRow(3);51 //row3.CreateCell(0).SetCellValue("姓名");

52

53 第四列

54 //NPOI.SS.UserModel.IRow row4 = sheet.CreateRow(4);55 //row4.CreateCell(0).SetCellValue("职务");

56

57 第五列

58 //NPOI.SS.UserModel.IRow row5 = sheet.CreateRow(5);59 //row5.CreateCell(0).SetCellValue("性别");

60

61 第六列

62 //NPOI.SS.UserModel.IRow row6 = sheet.CreateRow(6);63 //row6.CreateCell(0).SetCellValue("民族");

64

65 第七列

66 //NPOI.SS.UserModel.IRow row7 = sheet.CreateRow(7);67 //row7.CreateCell(0).SetCellValue("出生年月");

68

69 第八列

70 //NPOI.SS.UserModel.IRow row8 = sheet.CreateRow(8);71 //row8.CreateCell(0).SetCellValue("组");

72

73 第九列

74 //NPOI.SS.UserModel.IRow row9 = sheet.CreateRow(9);75 //row9.CreateCell(0).SetCellValue("备注");

76 #endregion

77 int rowIndex = 1;78

79 foreach (DataRow r indt.Rows)80 {81 NPOI.SS.UserModel.IRow dataRow =sheet.CreateRow(rowIndex);82 string str = r["StuDentIdCard"].ToString().Substring(6, 8);83 str = str.Substring(0, 4) + "-" + str.Substring(4, 2) + "-" + str.Substring(6, 2);84 dataRow.CreateCell(0).SetCellValue(rowIndex.ToString());85 dataRow.CreateCell(1).SetCellValue(r["StudentUnit"].ToString());86 dataRow.CreateCell(2).SetCellValue(r["StudentDepartment"].ToString());87 dataRow.CreateCell(3).SetCellValue(r["StudentName"].ToString());88 dataRow.CreateCell(4).SetCellValue(r["StudentPosition"].ToString());89 dataRow.CreateCell(5).SetCellValue(r["StudentGender"].ToString() == "1" ? "男" : "女");90 dataRow.CreateCell(6).SetCellValue(r["StudentNation"].ToString());91 dataRow.CreateCell(7).SetCellValue(str);92 dataRow.CreateCell(8).SetCellValue(r["TrainShiftNum"].ToString());93 dataRow.CreateCell(9).SetCellValue(r["Remark"].ToString());94 rowIndex++;95 }96

97 //写入到客户端

98 book.Write(ms);99 //Response.ClearContent();100 //Response.Clear();101 //Response.Buffer = true;102 //添加头信息,为"文件下载/另存为"对话框指定默认文件名

103 Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", System.DateTime.Now.ToString("yyyymmddhhmmss")));104 Response.Charset = "UTF-8";105 Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");106 //指定返回的是一个不能被客户端读取的流,必须被下载

107 Response.ContentType = "application/ms-excel";108 //把文件流发送到客户端

109 Response.BinaryWrite(ms.ToArray());110 book = null;111 ms.Close();112 ms.Dispose();113 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 使用NPOI可以很方便地在Winform应用程序中导出Excel文件。 首先,我们需要将NPOI引用添加到Winform项目中。可以通过NuGet包管理器或手动引用方式添加。 然后,我们需要创建一个工作簿对象,并添加一个工作表。可以使用HSSFWorkbook或XSSFWorkbook类来创建工作簿对象,分别对应xls和xlsx格式Excel文件。 接下来,我们可以向工作表中添加数据。可以使用工作表中的创建行对象,然后为每行添加单元格数据。可以设置单元格的值、格式、样式等属性。 最后,我们需要将工作簿保存为Excel文件。可以使用FileStream类创建一个文件流对象,并使用工作簿的Write方法将数据写入到文件流中。 以下是一个简单的示例代码,将一个包含学生信息的列表导出Excel文件: ```csharp using System; using System.Collections.Generic; using System.IO; using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; // 创建工作簿和工作表 HSSFWorkbook workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet("学生信息"); // 添加表头 IRow headerRow = sheet.CreateRow(0); headerRow.CreateCell(0).SetCellValue("学号"); headerRow.CreateCell(1).SetCellValue("姓名"); headerRow.CreateCell(2).SetCellValue("年龄"); // 添加数据 List<Student> students = GetStudents(); for (int i = 0; i < students.Count; i++) { IRow dataRow = sheet.CreateRow(i + 1); dataRow.CreateCell(0).SetCellValue(students[i].Id); dataRow.CreateCell(1).SetCellValue(students[i].Name); dataRow.CreateCell(2).SetCellValue(students[i].Age); } // 保存为Excel文件 using (FileStream fileStream = new FileStream("学生信息.xls", FileMode.Create)) { workbook.Write(fileStream); } ``` 在这个示例中,我们首先创建了一个工作簿和一个工作表,并添加了表头。然后,通过获取学生信息列表来添加数据。最后,我们将工作簿保存为名为“学生信息.xls”的Excel文件。 这样,使用NPOI就可以在Winform应用程序中导出Excel文件。希望可以对你有所帮助! ### 回答2: 使用WinForm搭配NPOI导出Excel非常简单。首先,我们需要在WinForm中添加对NPOI的引用。可以通过NuGet包管理器来导入NPOI库。 导入库后,我们可以创建一个DataGridView控件来展示需要导出的数据,或者直接在代码中定义一个DataTable对象来储存数据。然后,在按钮的Click事件处理程序中编写导出Excel的代码。 以下是一个简单的示例: 1. 添加一个DataGridView控件(或创建DataTable对象)并加载需要导出的数据。 2. 在按钮的Click事件中添加以下代码: ```csharp using NPOI.XSSF.UserModel; // 导入XSSF命名空间 using NPOI.SS.UserModel; // 导入SS命名空间 using NPOI.HSSF.Util; // 导入HSSFUtil命名空间 using NPOI.HSSF.UserModel; // 导入HSSFUserModel命名空间 using NPOI.SS.Util; // 导入SSUtil命名空间 using NPOI.HPSF; // 导入HPSF命名空间 using NPOI.POIFS.FileSystem; // 导入POIFS命名空间 // 创建一个Excel文档对象 XSSFWorkbook workbook = new XSSFWorkbook(); // 创建一个工作表对象 ISheet sheet = workbook.CreateSheet("Sheet1"); // 创建行和单元格 IRow row = sheet.CreateRow(0); for (int i = 0; i < dataGridView1.Columns.Count; i++) { row.CreateCell(i).SetCellValue(dataGridView1.Columns[i].HeaderText); } // 填充数据 for (int i = 0; i < dataGridView1.Rows.Count; i++) { row = sheet.CreateRow(i + 1); for (int j = 0; j < dataGridView1.Columns.Count; j++) { row.CreateCell(j).SetCellValue(dataGridView1.Rows[i].Cells[j].Value.ToString()); } } // 保存文件 SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.Filter = "Excel文件|*.xlsx"; if (saveFileDialog.ShowDialog() == DialogResult.OK) { using (FileStream fs = new FileStream(saveFileDialog.FileName, FileMode.Create)) { workbook.Write(fs); } } // 提示导出成功 MessageBox.Show("导出成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); ``` 这是一个基本的WinForm使用NPOI导出Excel的代码示例。你可以根据自己的需求进行修改和扩展。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值