当前位置:   article > 正文

.net Winfom NPOI导出Excel(弹出窗口自定义保存路径与文件名称)_winformnpoi如何实现将datatable导出为excel的时候可以弹窗选择保存位置和文件名

winformnpoi如何实现将datatable导出为excel的时候可以弹窗选择保存位置和文件名
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using NPOI.HSSF.UserModel;
  7. using System.IO;
  8. using System.Windows.Forms;
  9. using Model;
  10. using NPOI.XSSF.UserModel;//调用NPOI引用
  11. namespace DAL.Helper
  12. {
  13.     public class PrintHelper
  14.     {
  15.         public void PrintMsg(StudentModel objStudentModel)
  16.         {
  17.             //打开事先编写好的Excle模板,当前权限为只读
  18.             FileStream theExcel = new FileStream(@"E:\rcmeng\source\Test Program\AppOfStudents\AppOfStudents\bin\Debug\StudentInfo.xlsx", FileMode.Open, FileAccess.Read);
  19.             //调用Excle模板,并新建一个工作簿,计算机中如果Excle版本为2007以下则为HSSFWorkbook,若为2007以上,则为XSSFWorkbook
  20.             XSSFWorkbook theStudentInfo = new XSSFWorkbook(theExcel);
  21.             //新建或获取一个工作表,由于已经事先编写好了模板,可以直接调用模板中的工作表,此处所调用的工作表名称为“info”
  22.             XSSFSheet theStudentInfoSheet = (XSSFSheet)theStudentInfo.GetSheet("Info");
  23.             //NPOI操作,将封装好的数据(objStudentModel)依次填充进该Excle中
  24.             XSSFCell cellId = (XSSFCell)theStudentInfoSheet.GetRow(3).GetCell(3);
  25.             cellId.SetCellValue(objStudentModel.StudentId);
  26.             XSSFCell cellName = (XSSFCell)theStudentInfoSheet.GetRow(3).GetCell(5);
  27.             cellName.SetCellValue(objStudentModel.StudentName);
  28.             XSSFCell cellGender = (XSSFCell)theStudentInfoSheet.GetRow(3).GetCell(7);
  29.             cellGender.SetCellValue(objStudentModel.Gender);
  30.             XSSFCell cellClassName = (XSSFCell)theStudentInfoSheet.GetRow(5).GetCell(3);
  31.             cellClassName.SetCellValue(objStudentModel.ClassName);
  32.             XSSFCell cellPhoneNumber = (XSSFCell)theStudentInfoSheet.GetRow(5).GetCell(5);
  33.             cellPhoneNumber.SetCellValue(objStudentModel.PhoneNumber);
  34.             XSSFCell cellStudentAddress = (XSSFCell)theStudentInfoSheet.GetRow(7).GetCell(3);
  35.             cellStudentAddress.SetCellValue(objStudentModel.StudentAddress);
  36.             //保存文件
  37.             //提示用户保存文件的位置(引用的是IO的扩展文件)
  38.             SaveFileDialog saveFile = new SaveFileDialog();
  39.             //新文件默认扩展名,Excle文件可为.xlsx或.xls
  40.             saveFile.DefaultExt = ".xlsx";
  41.             //保存类型设置项(比较懒,所以只写了一个设置项)
  42.             saveFile.Filter = "Excel文件|*.xlsx";
  43.             //新文件名默认设置为“学员信息”
  44.             string fileName = "学员信息";
  45.             //将已设置好的默认名称赋值
  46.             saveFile.FileName = fileName;
  47.             //弹出对话框
  48.             saveFile.ShowDialog();
  49.             //可进行文件名修改,并保存
  50.             fileName = saveFile.FileName;
  51.             //点击取消
  52.             if (fileName.IndexOf(":") < 0)
  53.             {
  54.                 return;
  55.             }
  56.             //点击确定
  57.             if (fileName != "")
  58.             {
  59.                 //新建文件流,创建一个新文件
  60.                 FileStream newFile = new FileStream(saveFile.FileName, FileMode.Create);
  61.                 theStudentInfo.Write(newFile);
  62.                 //关闭文件流
  63.                 newFile.Close();
  64.                 //GC垃圾回收机制
  65.                 GC.SuppressFinalize(this);
  66.             }
  67.         }
  68.     }
  69. }

 

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号