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 的大小写留空但结果没有改变...
编辑:
我尝试删除整个程序,只留下这个:
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();
}
这是文档文件 FileMode,因为您可能更喜欢 Create 的替代方法。
我制作了一个程序,用于将一个单元格分成两个单元格并将它们写在不同的工作表中,但是在我 运行 它之后 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 的大小写留空但结果没有改变...
编辑: 我尝试删除整个程序,只留下这个:
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();
}
这是文档文件 FileMode,因为您可能更喜欢 Create 的替代方法。