简体   繁体   中英

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. After that I open file B and just add a litle bit data (for example: 1) to it and save and close it.
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.
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. Do you have any ideas? please share to me, thank you so much.
Remarks:
1. the computer use Office 2010 to open Excel file
2. the file format is Excel workbook (.xlsx)
3. if the computer installed office with later version (2013, 2016), the problem was not appeared.

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.

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.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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