FastExcel icon indicating copy to clipboard operation
FastExcel copied to clipboard

Update multiple worksheets at onces

Open piwonesien opened this issue 4 years ago • 3 comments

We have the situation that we want to fill an excel template that has several worksheets. Is there a better way to do this instead of:

using (FastExcel.FastExcel fastExcel = new FastExcel.FastExcel(templateFile, outputFileTmp))
{
    fastExcel.Write(worksheet1, "sheet1", 1);
}
using (FastExcel.FastExcel fastExcel = new FastExcel.FastExcel(new FileInfo(outputPathTmp), outputFile))
{
    fastExcel.Write(worksheet2, "sheet2", 1);
}
File.Delete(outputPathTmp);

piwonesien avatar Jun 16 '20 13:06 piwonesien

I haven't tested this but this should work

using (FastExcel.FastExcel fastExcel = new FastExcel.FastExcel(templateFile, outputFileTmp))
{
    fastExcel.Write(worksheet1, "sheet1", 1);
    fastExcel.Write(worksheet2, "sheet2", 1);
}
File.Delete(outputPathTmp);

mrjono1 avatar Jun 19 '20 10:06 mrjono1

@mrjono1 yeah, it would be nice if this solution would work (that was also my first idea), but I'll get an error then:

System.Exception: 'Could not copy template to output file path'

IOException: The file '{...}' already exists.

piwonesien avatar Jun 19 '20 12:06 piwonesien

Currently you have to reopen the file as a workaround.

using (FastExcel.FastExcel fastExcel = new FastExcel.FastExcel(templateFile, outputFileTmp)) { fastExcel.Write(worksheet1, "sheet1", 1); } using (FastExcel.FastExcel fastExcel = new FastExcel.FastExcel(outputFileTmp)) { fastExcel.Write(worksheet2, "sheet2", 1); }

TorstenL avatar Jul 07 '20 05:07 TorstenL