簡體   English   中英

NPOI文件損壞

[英]NPOI Corrupted file

我制作了一個程序,用於將一個單元格拆分為兩個單元格,並將它們寫在不同的工作表中,但是運行它后,excel文件被損壞。

IWorkbook workbook;
            using(FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                workbook = new XSSFWorkbook(stream);
            }
            IWorkbook newWorkbook = new XSSFWorkbook();

            ISheet sheet = workbook.GetSheetAt(0);
            ISheet oneWordSheet = newWorkbook.CreateSheet();
            ISheet moreWordsSheet = newWorkbook.CreateSheet();
            IRow tmpRow;
            for(int i = 5; i < 100/*sheet.LastRowNum*/ + 1; i++)
            {
                tmpRow = sheet.GetRow(i);
                string[] strings = tmpRow.GetCell(2).StringCellValue.Split(' ');
                string companyName = strings[0];
                bool parseFailed = true;
                for(int j = 1; parseFailed && j < strings.Length; j++)
                {
                    try
                    {
                        int.Parse(strings[j]);
                        parseFailed = false;
                    }
                    catch (FormatException)
                    {
                        companyName += strings[j];
                        j++;
                    }
                }
                tmpRow.CreateCell(4).SetCellValue(companyName);
                if(companyName.Trim().Split(' ').Length < 2)
                {
                    copyRowToSheet(tmpRow, oneWordSheet);
                }
                else
                {
                    copyRowToSheet(tmpRow, moreWordsSheet);
                }
            }
            using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Write))
            {
                newWorkbook.Write(stream);
            }

我做了這樣的copyRowToSheet方法。 應該是對的


private static void copyRowToSheet(IRow row, ISheet sheet)
        {
            IRow newRow = sheet.CreateRow(sheet.LastRowNum + 1);
            newRow.CreateCell(0).SetCellValue(row.GetCell(0).NumericCellValue);
            newRow.CreateCell(1).SetCellValue(row.GetCell(1).StringCellValue);
            newRow.CreateCell(2).SetCellValue(row.GetCell(4).StringCellValue);
            newRow.CreateCell(3).SetCellValue(row.GetCell(2).StringCellValue);
            newRow.CreateCell(4).SetCellValue(row.GetCell(3).StringCellValue);
        }

我嘗試從工作簿而不是newWorkbook進行寫操作,但是它仍然損壞了文件,我還嘗試了刪除copyRowToSheet方法(只是將if和else case都保留為空,但結果沒有改變...

編輯:我試圖刪除程序的整個主體,只剩下這樣:

IWorkbook workbook;
            using(FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                workbook = new XSSFWorkbook(stream);
                stream.Close();
            }
            IWorkbook newWorkbook = new XSSFWorkbook();


            using (FileStream stream = new FileStream(path, FileMode.Open, FileAccess.Write))
            {
                workbook.Write(stream);
                stream.Close();
            }

如果我沒記錯,這應該只讀取文件,然后將其保存回去而不進行任何編輯,但是它仍然會損壞文件

幾周前,我剛接觸npoi時就遇到了同樣的問題。 由於您正在使用的代碼在教程和博客中一再重復,因此很難診斷。

當您創建第二個FileStream以便將電子表格寫回到磁盤時,會發生此問題。 您正在寫入與先前閱讀的文件相同的文件。

寫入現有文件時,FileMode.Open的行為是將數據附加到文件末尾。 這將導致您在一個文件中有2個excel電子表格,當您打開該文件時,它們被聲明為損壞。

另一方面,FileMode.Create會覆蓋現有文件,因此這很可能是您所需要的。

using (FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write))
{
     workbook.Write(stream);
     stream.Close();
}

這是docs文件FileMode,因為您可能更喜歡Create的替代方法。

文件模式文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM