C#用自带插件导出Excel

1.导出excel用是vs自带的插件

建好项目之后,拖一个button过来。npoi是vs自带的插件,需要下载引入

所以需要添加npoi的程序包,在VS里以此选择,工具==》NuGet包管理器==》

解决方案的NuGet程序包,里面选择浏览,输入Npoi,

选择第一个,右边勾选一下,然后选择安装即可。

安装好之后可以看到多了几个文件。

2.写代码

好了接下来可以开始写带代码了。双击button进入到button的点击事件中。在cs文件中我们先引入几个命名空间。

using NPOI.SS.UserModel;
using System.IO;
using NPOI.XSSF.UserModel;

 public void xlsx()
        {
            string sql = string.Format("查询数据的sql语句");//导出的数据是从数据库里面读取出来的
            DataSet ds = SqlHelper.ExecuteDataset(ConnectionString, CommandType.Text, sql, null);//数据库查询类的方法及传入的参数,根据自己的数据库连接类查询数据就可以,这里显示的是自己用的数据库连接类
            DataTable table = ds.Tables[0];
            XSSFWorkbook workBook = new XSSFWorkbook();  //实例化XSSF
            XSSFSheet sheet = (XSSFSheet)workBook.CreateSheet();  //创建一个sheet

            IRow frow0 = sheet.CreateRow(0);  // 添加一行(一般第一行是表头)
            frow0.CreateCell(0).SetCellValue("设备名称");//表头显示的字段
            frow0.CreateCell(1).SetCellValue("设备编号");
            frow0.CreateCell(2).SetCellValue("状态");
            frow0.CreateCell(3).SetCellValue("时间");
            for (int i = 0; i < table.Rows.Count; i++)  //循环添加list中的内容放到表格里
            {
                IRow frow1 = sheet.CreateRow(i + 1);  //从i+1开始 因为第一行已经有表头了
                frow1.CreateCell(0).SetCellValue(table.Rows[i]["SystemID"].ToString());//读取导出的值,分别对应上面的表头显示
                frow1.CreateCell(1).SetCellValue(table.Rows[i]["UserID"].ToString());
                frow1.CreateCell(2).SetCellValue(table.Rows[i]["Status"].ToString());
                frow1.CreateCell(3).SetCellValue(table.Rows[i]["Time"].ToString());
            }

   //如果需要转换输再导出的话,

 //转换UserID的值,判断它的是等于几,然后给它重新赋值,如果有更好的方法,请留言交流

 //            if (table.Rows[i]["UserID"].ToString()=="1" 

 //            { 

 //                  frow1.CreateCell(1).SetCellValue("1代表的是优秀");//导出转换后的值
 //             }


            string saveFileName = "D:\\exce文件\\报表.xlsx";//位置
            try
            {
                using (FileStream fs = new FileStream(saveFileName, FileMode.Create, FileAccess.Write))
                {
                    workBook.Write(fs);  //写入文件
                    workBook.Close();  //关闭
                }
                MessageBox.Show("导出成功");
            }
            catch (Exception)
            {
                workBook.Close();
                MessageBox.Show("导出失败");
            }
        }

参考

 private void Button_Click(object sender, RoutedEventArgs e)
        {
            List<People> peoples = Peoplelist();
            XSSFWorkbook workBook = new XSSFWorkbook();  //实例化XSSF
            XSSFSheet sheet = (XSSFSheet)workBook.CreateSheet();  //创建一个sheet

            IRow frow0 = sheet.CreateRow(0);  // 添加一行(一般第一行是表头)
            frow0.CreateCell(0).SetCellValue("序号");
            frow0.CreateCell(1).SetCellValue("姓名");
            frow0.CreateCell(2).SetCellValue("年龄");   //表头内容

            for (int i = 0; i < peoples.Count; i++)  //循环添加list中的内容放到表格里
            {
                IRow frow1 = sheet.CreateRow(i+1);  //之所以从i+1开始 因为第一行已经有表头了
                frow1.CreateCell(0).SetCellValue(peoples[i].ID);
                frow1.CreateCell(1).SetCellValue(peoples[i].Name);
                frow1.CreateCell(2).SetCellValue(peoples[i].Age);
            }
            string saveFileName = "E:\\" + "Excel\\" + "people" + ".xlsx";
            try
            {
                using (FileStream fs = new FileStream(saveFileName, FileMode.Create, FileAccess.Write))
                {
                    workBook.Write(fs);  //写入文件
                    workBook.Close();  //关闭
                }
                MessageBox.Show("导出成功");
            }
            catch (Exception)
            {
                workBook.Close();
            }
        }

猜你喜欢

转载自blog.csdn.net/weixin_39237340/article/details/90646383