简体   繁体   中英

Create new excel spreadsheet document using FileStream not MemoryStream in OpenXml

I am writing code for creating excel spreadsheet using OpenXml. But wanted to use Filestream instead of MemoryStream in SpreadSheet.Create() method.

Note: With MemoryStream working correctly but for some reasons I need to get code working with Filestream.

Generating corrupted file when writing code like:

FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate);
using (SpreadsheetDocument document = 
SpreadsheetDocument.Create(fs,SpreadsheetDocumentType.Workbook))
{
 //worksheet code
}

If needed I can post worksheet code.

Try to use using to ensure that the file stream will be closed.

using (FileStream fs = new FileStream(filepath, FileMode.OpenOrCreate))
{
    using (SpreadsheetDocument document = 
SpreadsheetDocument.Create(fs,SpreadsheetDocumentType.Workbook))
    {
    //worksheet code
    }
}

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