NOPI通过模板导出excel——winform版

随笔只是用来记录个人开发中遇到的问题.

  1. 使用模板导出excel,处理复杂表头个样式的excel比较方便。
  2. 要注意文件夹的权限。防止造成创建临时文件时不能保存。
  3. 引用nopi。

    using NPOI.SS.UserModel;
    using NPOI.HSSF.UserModel;
    using NPOI.XSSF.UserModel;

    string filepath = "";//临时副本路径
                string fileName = "";//保存要存放的路径
                #region  打开保存窗口  然后保存数据
                SaveFileDialog fileDialog = new SaveFileDialog();
                fileDialog.DefaultExt = ".xlsx";
                fileDialog.Filter = "Excel文件|*.xlsx";
                if (fileDialog.ShowDialog() == DialogResult.OK)
                {
    
                    try
                    {
    
    
                        fileName = fileDialog.FileName;
                        if (data.Rows.Count > 0)
                        {
                            string filename = "KQPF" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xlsx"; //临时文件名称
                            string masterPath = Path.GetFullPath(@".../" + "模板.xlsx");//找到excel模板
                            int index = masterPath.IndexOf('');
                            //存放临时路径
                            filepath = masterPath.Substring(0, index) + filename;
                            //复制excel模板
                            File.Copy(masterPath, filepath);
                            //把文件属性读取
                            FileAttributes attrs = File.GetAttributes(filepath);
                            //下面表达式中的1 是FileAttributes.readonly 的值
                            //此表达式时把readnoly所在的位改成0
                            attrs = (FileAttributes)((int)attrs & ~(1));
                            FileStream file = new FileStream(filepath, FileMode.Open, FileAccess.Read);
                            XSSFWorkbook workbook = new XSSFWorkbook(file);
                            ISheet sheet = workbook.GetSheet("《稳定性测试报告》");
                            #region  第一行 "实验记录本编号"也就是报告号
                            IRow row1 = sheet.GetRow(1);//使用GetRow && GetCell 保持原有样式不变
                            row1.GetCell(10).SetCellValue("BG20191022001");
                            #endregion
                            sheet.ForceFormulaRecalculation = true;
                            #region 开始从副本保存
                            using (FileStream stream = File.OpenWrite(fileName))
                            {
                                workbook.Write(stream);
                                stream.Close();
                            }
                            MessageBox.Show("导出成功");
                            #endregion
                        }
                    }
                    catch (Exception)
                    {
                        MessageBox.Show("保存失败,请重试");
                        throw;
                    }
                    finally
                    {
                        File.Delete(filepath);//删除副本文件
                        GC.Collect();
                    }
                }
                else
                {
                    MessageBox.Show("用户点击了取消");
                }
                #endregion
                return null;

  4. 模板

      

猜你喜欢

转载自www.cnblogs.com/cyy12138/p/12517034.html
今日推荐