Link to home
Start Free TrialLog in
Avatar of zimmer9
zimmer9Flag for United States of America

asked on

Using C#, how to write out a new row with a cell containing the value "END OF REPORT" after a RESULT SET exported to Excel?

I am wring a C# Windows platform application using VS2005.
 
I use the following code to write out the RESULT SET of a SQL Server table to an Excel file.

I wish to write out the string "END OF REPORT" after the last record of the table is exported to the Excel file.

However, I commented out my 2 lines of code to write out a record with the value "END OF REPORT" because I was getting the error:
-----------------------
Problems came up in the following areas during load:
Table
This file cannot be opened because of errors. Errors are listed in C:\Documents and Settings\<user name>\Local Settings\Temporary Internet Files\Content.MSO\69160819.log

----------------------

How would you write a new <ss:/Row> using the existing code below
for a cell with the value "END OF REPORT",
since it would be outside of the </ss:Row> in the following code?
 
A cell cannot stand by itself--it needs to be part of a row.
---------------------------------------------------------------------------------
<ss:Row>
     <ss:Cell ss:StyleID="General">
        <ss:Data ss:Type="String">END OF REPORT</ss:Data>
      </ss:Cell>
</ss:Row>

---------------------------------------------------------------------------------

                        //Write header row
                        writer.WriteStartRow();
                        foreach (DataColumn col in data.Columns)
                            writer.WriteExcelUnstyledCell(col.Caption);
                        writer.WriteEndRow();

                        //write data
                        foreach (DataRow row in data.Rows)
                        {
                            writer.WriteStartRow();
                            foreach (object o in row.ItemArray)
                            {
                                writer.WriteExcelAutoStyledCell(o);
                            }
                            writer.WriteEndRow();
                        }

                        //write END OF REPORT at the end
                        //writer.WriteExcelAutoStyledCell("END OF REPORT");  //commented out
                        //writer.Wr iteEndRow();                              //commented out
                       

                        // Close up the document
                        writer.WriteEndWorksheet();
                        writer.WriteEndDocument();    //<--this statement generates an Excpetion
                        writer.Close();
---------------------------------------------------

The last few lines of output generated by the C# code as it stands now is as follows:

<ss:Cell ss:StyleID="General">
          <ss:Data ss:Type="String">N</ss:Data>
        </ss:Cell>
        <ss:Cell ss:StyleID="General">
          <ss:Data ss:Type="String">+0</ss:Data>
        </ss:Cell>
        <ss:Cell ss:StyleID="General">
          <ss:Data ss:Type="String">   </ss:Data>
        </ss:Cell>
        <ss:Cell ss:StyleID="General">
          <ss:Data ss:Type="String">Y</ss:Data>
        </ss:Cell>
        <ss:Cell ss:StyleID="General">
          <ss:Data ss:Type="String">0</ss:Data>
        </ss:Cell>
      </ss:Row>
      <ss:Cell ss:StyleID="General">
        <ss:Data ss:Type="String">END OF REPORT</ss:Data>
      </ss:Cell>
    </ss:Table>
  </ss:Worksheet>
</ss:Workbook>
Avatar of Dmitry G
Dmitry G
Flag of New Zealand image

I believe you just need to create a row and write your message to the first cell.

By the way, no one knows what writes etc you are using. It would be helpful to know a bit more. I believe you are using code from: http://stackoverflow.com/questions/373925/c-sharp-winforms-app-export-dataset-to-excel . Aren't you?

OK, try to insert my code instead your two commented out lines. I didn't test it of course because I have no your setup :). But it should work.

            //basically, we need to know how many cells in a row and use for loop
            // or can do this way:
            DataRow row  =  data.Rows[0]; //just take the first row, e.g.

            writer.WriteStartRow();// start new row
            int loopCounter = 0;
            foreach (object o in row.ItemArray)
            {
                if (loopCounter == 0)
                {
                    writer.WriteExcelAutoStyledCell("END OF REPORT"); // write first cell not blank
                    loopCounter += 1; // increase the counter
                }
                else
                {
                    writer.WriteExcelAutoStyledCell(""); // write a blank cells
                }
            }
            writer.WriteEndRow(); // conclude the row

Open in new window

Avatar of zimmer9

ASKER

Error      1      A local variable named 'row' cannot be declared in this scope because it would give a different meaning to 'row', which is already used in a 'child' scope to denote something else      U:\Visual Studio 2005\Projects\UDLApplication\UDLApplication\Form1.cs      982      33      UDLApplication


public static void ExcelExport(DataTable data, String fileName)
        {
            //export a DataTable to Excel
            DialogResult retry = DialogResult.Retry;

            while (retry == DialogResult.Retry)
            {
                try
                {
                    using (ExcelWriter writer = new ExcelWriter(fileName))
                    {
                        writer.WriteStartDocument();

                        // Write the worksheet contents
                        writer.WriteStartWorksheet("Sheet1");

                        //Write header row
                        writer.WriteStartRow();
                        foreach (DataColumn col in data.Columns)
                            writer.WriteExcelUnstyledCell(col.Caption);
                        writer.WriteEndRow();

                        //write data
                       
                        foreach (DataRow row in data.Rows)
                        {
                            writer.WriteStartRow();
                            foreach (object o in row.ItemArray)
                            {
                                writer.WriteExcelAutoStyledCell(o);
                            }
                            writer.WriteEndRow();
                        }
                       

                        //basically, we need to know how many cells in a row and use for loop
                        // or can do this way:
                        DataRow row = data.Rows[0]; //just take the first row, e.g.  //Error **

                        writer.WriteStartRow();// start new row
                        int loopCounter = 0;
                        foreach (object o in row.ItemArray)
                        {
                            if (loopCounter == 0)
                            {
                                writer.WriteExcelAutoStyledCell("END OF REPORT"); // write first cell not blank
                                loopCounter += 1; // increase the counter
                            }
                            else
                            {
                                writer.WriteExcelAutoStyledCell(""); // write a blank cells
                            }
                        }
                        writer.WriteEndRow(); // conclude the row

                        //write END OF REPORT at the end
                        //writer.WriteExcelAutoStyledCell("END OF REPORT");
                        //writer.WriteEndRow();

                        // Close up the document
                        writer.WriteEndWorksheet();
                        writer.WriteEndDocument();
                        writer.Close();

                        retry = DialogResult.Cancel;
                    }
                }
                catch (Exception myException)
                {
                    //myException.Message;
                    retry = MessageBox.Show(myException.Message, "Excel Export", MessageBoxButtons.RetryCancel, MessageBoxIcon.Asterisk);
                }
            }
        }
ASKER CERTIFIED SOLUTION
Avatar of Miguel Oz
Miguel Oz
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial