Saving Excel files from C# - How to get rid of Compat dialog

Tom Archer

New Member
Joined
Feb 9, 2008
Messages
8
I have the following code that saves the contents of a ListView to an Excel spreadsheet. This all works. However, when I added code to format the data as a table, I now get the Excel Compatibility dialog upon calling SaveAs. Is there a way to format the table or save the spreadsheet such that I do not get this dialog?


Code:
    string WriteExcelFile()
    {
      Excel.Application xlApp;
      Excel.Workbook xlWorkBook;
      Excel.Worksheet xlWorkSheet;
      object misValue = System.Reflection.Missing.Value;
      xlApp = new Excel.ApplicationClass();
      xlWorkBook = xlApp.Workbooks.Add(misValue);
      xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
      // Get the enumerator to collection of child nodes of selected node
      IEnumerator enumerator = lstData.Items.GetEnumerator();
      // Header columns
      xlWorkSheet.Cells[1, 1] = TITLE;
      xlWorkSheet.Cells[1, 2] = PAGEVIEWS;
      xlWorkSheet.Cells[1, 3] = URL;
      // For each ListView item...
      int row = 2;
      while (enumerator.MoveNext())
      {
        ListViewItem li = enumerator.Current as ListViewItem;
        PageInfo pageInfo = li.Tag as PageInfo;
        // Update spreadsheet
        xlWorkSheet.Cells[row, 1] = pageInfo.Title;
        xlWorkSheet.Cells[row, 2] = pageInfo.PageViews.ToString();
        xlWorkSheet.Cells[row, 3] = pageInfo.Url;
 
        row++;
      }
      // Format data as a Table
      StringBuilder endRange = new StringBuilder();
      endRange.AppendFormat("C{0}", row);
      Excel.Range range = xlWorkSheet.get_Range("A1", endRange.ToString());
      xlWorkSheet.ListObjects.Add(Excel.XlListObjectSourceType.xlSrcRange, 
                                  range,
                                  Type.Missing, 
                                  Excel.XlYesNoGuess.xlYes, 
                                  Type.Missing).Name = "TestTable";
      xlWorkSheet.ListObjects["TestTable"].TableStyle = "TableStyleMedium3";
      // Create a uniquely named file
      string fileName = GetRandomFileName(FILE_EXTENSION_EXCEL);
      xlWorkBook.SaveAs(fileName, 
                        Excel.XlFileFormat.xlWorkbookNormal, 
                        misValue, 
                        misValue, 
                        misValue, 
                        misValue, 
                        Excel.XlSaveAsAccessMode.xlExclusive, 
                        misValue, 
                        misValue, 
                        misValue, 
                        misValue, 
                        misValue);
      xlWorkBook.Close(true, misValue, misValue);
      xlApp.Quit();
      ReleaseComObject(xlWorkSheet);
      ReleaseComObject(xlWorkBook);
      ReleaseComObject(xlApp);
 

Excel Facts

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
Welcome to the Board Tom!

I don't know anything about C, but maybe you can use Application.DisplayAlerts = False?
 
Upvote 0
Hey Smitty! I figured that I'd see some old friends around here :)

Thanks much for the tip. That did the trick perfectly!
 
Upvote 0
Glad I could help you for once!

Hope all's well in your neck of the woods - Cyndi says "Hi!".
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,906
Members
452,366
Latest member
TePunaBloke

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top