martin_tang 2016-08-10 09:37 采纳率: 0%
浏览 1670
已采纳

使用NPOI添加excel內容失敗(在現有excel檔案后添加數據失敗)

通過click事件,在檔案中新建d:\a1.xlsx這個文件,也可以保存數據;但是重新打開,添加數據失敗!
下面使用了兩種NPOI方式,全部失敗!!沒有報錯,也沒發現問題所在。
執行后都是只能顯示如下內容,也就是說可以新建並且存入,但是,但是,但是在現有excel后添加數據失敗,請大家幫忙看看,指教!
图片说明

 private void button51_Click(object sender, EventArgs e)
        {

            NPOI.XSSF.UserModel.XSSFWorkbook book = new NPOI.XSSF.UserModel.XSSFWorkbook();
            NPOI.SS.UserModel.ISheet sheet = book.CreateSheet("test_01");

            if (rowIndex == 1)       //執行一次
            {
                NPOI.SS.UserModel.IRow headerRow = sheet.CreateRow(0);
                headerRow.CreateCell(0).SetCellValue("序號");
                headerRow.CreateCell(1).SetCellValue("測試一");
                headerRow.CreateCell(2).SetCellValue("測試二");
                headerRow.CreateCell(3).SetCellValue("測試三");
                headerRow.CreateCell(4).SetCellValue("測試四");

                NPOI.SS.UserModel.IRow dataRow = sheet.CreateRow(rowIndex);
                dataRow.CreateCell(0).SetCellValue(rowIndex.ToString());
                dataRow.CreateCell(1).SetCellValue(userControl_station_status6.textbox94_int.ToString());
                dataRow.CreateCell(2).SetCellValue(userControl_station_status8.textbox93_int.ToString());
                dataRow.CreateCell(3).SetCellValue(userControl_station_status10.textbox92_int.ToString());
                dataRow.CreateCell(4).SetCellValue(userControl_station_status12.textbox91_int.ToString());
                fs = new FileStream(@"d:\a1.xlsx", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);

                book.Write(fs);
                //fs.Flush();
                headerRow = null;
                dataRow = null;
                sheet = null;
                book.Close();
                book = null;
                book = null;
                book = null;
                fs.Close();
                fs.Dispose();
                 fs = null;
            }
            else
            {
                FileStream fs = new FileStream(@"d:\a1.xlsx", FileMode.Open, FileAccess.ReadWrite);
                XSSFWorkbook workbook = new NPOI.XSSF.UserModel.XSSFWorkbook(fs);
                ISheet sheet2 = workbook.GetSheetAt(0);//获取工作表
                IRow row = sheet2.GetRow(0); //得到表头
                row = sheet2.CreateRow((sheet2.LastRowNum + 1));//在工作表中添加一行

                row.CreateCell(0).SetCellValue("ftfyfghghvgh");
                row.CreateCell(1).SetCellValue("ftfyfghghvgh");             
                workbook.Write(fs);//写入文件
                workbook.Close();
                workbook = null;
                fs.Close();
                fs.Dispose();
                fs = null;
            }
       }









 private void button51_Click(object sender, EventArgs e)
        {

            NPOI.XSSF.UserModel.XSSFWorkbook book = new NPOI.XSSF.UserModel.XSSFWorkbook();
            NPOI.SS.UserModel.ISheet sheet = book.CreateSheet("test_01");

            if (rowIndex == 1)       //執行一次
            {
                NPOI.SS.UserModel.IRow headerRow = sheet.CreateRow(0);
                headerRow.CreateCell(0).SetCellValue("序號");
                headerRow.CreateCell(1).SetCellValue("測試一");
                headerRow.CreateCell(2).SetCellValue("測試二");
                headerRow.CreateCell(3).SetCellValue("測試三");
                headerRow.CreateCell(4).SetCellValue("測試四");

                NPOI.SS.UserModel.IRow dataRow = sheet.CreateRow(rowIndex);
                dataRow.CreateCell(0).SetCellValue(rowIndex.ToString());
                dataRow.CreateCell(1).SetCellValue(userControl_station_status6.textbox94_int.ToString());
                dataRow.CreateCell(2).SetCellValue(userControl_station_status8.textbox93_int.ToString());
                dataRow.CreateCell(3).SetCellValue(userControl_station_status10.textbox92_int.ToString());
                dataRow.CreateCell(4).SetCellValue(userControl_station_status12.textbox91_int.ToString());
                fs = new FileStream(@"d:\a1.xlsx", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);

                book.Write(fs);
                //fs.Flush();
                headerRow = null;
                dataRow = null;
                sheet = null;
                book.Close();
                book = null;
                book = null;
                book = null;
                fs.Close();
                fs.Dispose();
                 fs = null;
            }
            else
            {



                NPOI.SS.UserModel.IRow dataRow = sheet.CreateRow(rowIndex);
                dataRow.CreateCell(0).SetCellValue(rowIndex.ToString());
                dataRow.CreateCell(1).SetCellValue(userControl_station_status6.textbox94_int.ToString());
                dataRow.CreateCell(2).SetCellValue(userControl_station_status8.textbox93_int.ToString());
                dataRow.CreateCell(3).SetCellValue(userControl_station_status10.textbox92_int.ToString());
                dataRow.CreateCell(4).SetCellValue(userControl_station_status12.textbox91_int.ToString());
                fs = new FileStream(@"d:\a1.xlsx", FileMode.Append);//写入流
                book.Write(fs);
                dataRow = null;
                sheet = null;
                book.Close();
                book = null;
                book = null;
                book = null;
                fs.Close();
                fs.Dispose();
                fs = null;
            }


            rowIndex++;


        }
    }

  • 写回答

5条回答 默认 最新

  • martin_tang 2016-08-12 07:52
    关注
       //  方法 1
                    FileStream fs = new FileStream(@"d:\a1.xlsx", FileMode.Open, FileAccess.ReadWrite);
                  //  XSSFWorkbook workbook = new NPOI.XSSF.UserModel.XSSFWorkbook(fs);
                    XSSFWorkbook  workbook = new XSSFWorkbook(fs);
                    ISheet sheet2 = workbook.GetSheetAt(0);//获取工作表
                    IRow row = sheet2.GetRow(0); //得到表头
                    row = sheet2.CreateRow((sheet2.LastRowNum + 1));//在工作表中添加一行
    
                    row.CreateCell(0).SetCellValue((sheet2.LastRowNum).ToString());
                    row.CreateCell(1).SetCellValue(userControl_station_status6.textbox94_int.ToString());
                    row.CreateCell(2).SetCellValue(userControl_station_status6.textbox93_int.ToString());
                    row.CreateCell(3).SetCellValue(userControl_station_status6.textbox92_int.ToString());
                    row.CreateCell(4).SetCellValue(userControl_station_status6.textbox91_int.ToString());
    
                    FileStream fs2 = new FileStream(@"d:\a1.xlsx", FileMode.Create, FileAccess.Write);
                    workbook.Write(fs2);
    
                  //  workbook.Write(fs);//写入文件
                    workbook.Close();
                    workbook = null;
                    fs.Close();
                    fs.Dispose();
                    fs = null;
    
                    fs2.Close();
                    fs2.Dispose();
                    fs2 = null;
    
    

    使用兩次“流”就能實現。分數給自己吧!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 NAO机器人的录音程序保存问题
  • ¥15 C#读写EXCEL文件,不同编译
  • ¥15 如何提取csv文件中需要的列,将其整合为一篇完整文档,并进行jieba分词(语言-python)
  • ¥15 MapReduce结果输出到HBase,一直连接不上MySQL
  • ¥15 扩散模型sd.webui使用时报错“Nonetype”
  • ¥15 stm32流水灯+呼吸灯+外部中断按键
  • ¥15 将二维数组,按照假设的规定,如0/1/0 == "4",把对应列位置写成一个字符并打印输出该字符
  • ¥15 NX MCD仿真与博途通讯不了啥情况
  • ¥15 win11家庭中文版安装docker遇到Hyper-V启用失败解决办法整理
  • ¥15 gradio的web端页面格式不对的问题