So I am doing a few For loops that iterate through different variable changes imported from a template, a SQL script run, data is dumped, loop to next SQL script, etc, and this is done multiple times using the same template file (which at the end of the query dumps is saved as a new file. With that as background:
I need to be able to open the template the first time through the loop, then keep it open through each query dump until done. I don't want to keep reopening the file as its just too big and cumbersome I have this so far:
public void ExportToExcel(DataSet dataSet, string templatePath)
{
Excel.Application excelApp = new Excel.Application();
excelApp.Visible = true;
FileInfo excelFileInfo = new FileInfo(templatePath);
Boolean fileOpenTest = IsFileLocked(excelFileInfo);
if (!fileOpenTest)
{
Excel.Workbook templateBook = excelApp.Workbooks.Open(templatePath);
}
else
{
Excel.Workbook templateBook = excelApp.Workbooks[templatePath];
}
for (int i = 0; i < lstQueryDumpSheet.Items.Count; i++)
{
string tabName = lstQueryDumpSheet.Items[i].ToString();
Excel.Worksheet templateSheet = templateBook.Sheets[tabName];
// Copy DataTable
foreach (System.Data.DataTable dt in dataSet.Tables)
{ ... rest of loops...
My Problem is that the code line "Excel.Worksheet templateSheet = templateBook.Sheets[tabName];" tells me that "templateBook" is not assigned, but I am assigning it outside the IF statement so it should pass....right?
Aucun commentaire:
Enregistrer un commentaire