简体   繁体   English

无法编辑生成的Excel文件

[英]Cannot edit generated excel file

Problem : 问题
In installed Office 2010 computer , my app have to copy an empty excel file (file A) to new excel file (file B) and use OpenXML library (V2.5) to execute some action, finally saved to hard disk. 在已安装的Office 2010计算机中 ,我的应用程序必须将一个空的excel文件(文件A)复制到新的excel文件(文件B),然后使用OpenXML库(V2.5)执行某些操作,最后保存到硬盘上。 After that I open file B and just add a litle bit data (for example: 1) to it and save and close it. 之后,我打开文件B,然后向其中添加一比特数据(例如:1),然后保存并关闭它。
when I reopen file B, excel thrown an error: Excel found unreadable content in ' file B' do you want to recover the contents of this workbook... and I can not open it. 当我重新打开文件B时,excel引发了一个错误: Excel在“文件B”中发现了不可读的内容,您是否要恢复此工作簿的内容...而我无法打开它。
Below is my code: 下面是我的代码:

 static void Main(string[] args) { ExportDataSet(@"C:\\A.xlsx",@"C:\\"); } public static void Copy(String oldPath, String newPath) { FileStream input = null; FileStream output = null; try { input = new FileStream(oldPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); output = new FileStream(newPath, FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite); var buffer = new byte[32768]; int read; while ((read = input.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, read); } } catch (Exception e) { } finally { if (input != null) { input.Close(); input.Dispose(); } if (output != null) { output.Close(); output.Dispose(); } } } public static string ExportDataSet(string filePath, string path, int startRow = 10) { var pathToSave = path; if (!Directory.Exists(pathToSave)) Directory.CreateDirectory(pathToSave); var filename = pathToSave + Guid.NewGuid() + Path.GetExtension(filePath); Copy(filePath, filename); var fs = File.Open(filename, FileMode.Open); { using (var myWorkbook = SpreadsheetDocument.Open(fs, true)) { var workbookPart = myWorkbook.WorkbookPart; var Sheets = myWorkbook.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>(); var relationshipId = Sheets.First().Id.Value; var worksheetPart = (WorksheetPart)myWorkbook.WorkbookPart.GetPartById(relationshipId); var sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>(); workbookPart.Workbook.Save(); //workbookPart.Workbook.CalculationProperties = new CalculationProperties() { FullCalculationOnLoad = true }; } fs.Close(); fs.Dispose(); return filename; } } 
I think the OpenXML library has something wrong. 我认为OpenXML库有问题。 Do you have any ideas? 你有什么想法? please share to me, thank you so much. 请分享给我,非常感谢。
Remarks: 备注:
1. the computer use Office 2010 to open Excel file 1.在计算机上使用Office 2010打开Excel文件
2. the file format is Excel workbook (.xlsx) 2.文件格式为Excel工作簿(.xlsx)
3. if the computer installed office with later version (2013, 2016), the problem was not appeared. 3.如果计算机安装了具有更高版本(2013、2016)的Office,则不会出现此问题。

Your buffer reading and writting logic is wrong. 您的缓冲区读取和写入逻辑是错误的。 The second parameter is where it starts to read or write and you are passing it a zero value, so second iteration of the while is overwritting the content written in the first iteration thus you are getting corrupted data if the files are greater than your buffer size. 第二个参数是它开始读取或写入的位置,并且您要向其传递一个零值,因此while的第二次迭代将覆盖第一次迭代中写入的内容,因此,如果文件大于缓冲区大小,则将损坏数据。 。

Your code should be similar to this: 您的代码应与此类似:

var buffer = new byte[32768];
int totalRead = 0; // Variable to track where to write in the next while iteration
int read;
while ((read = input.Read(buffer, totalRead, buffer.Length)) > 0)
{
    output.Write(buffer, totalRead, read);
    totalRead += read; // Add to totalRead the amount written so next iteration does append the content at the end.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM