程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> xlsx-使用NPOI添加excel內容失敗(在現有excel檔案後添加數據失敗)

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

編輯:編程解疑
使用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++;


        }
    }

最佳回答:


   //  方法 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;

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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved