FastExcel
FastExcel copied to clipboard
Update multiple worksheets at onces
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);
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 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.
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); }