zimmer9
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.WriteExcelUnstyledC ell(col.Ca ption);
writer.WriteEndRow();
//write data
foreach (DataRow row in data.Rows)
{
writer.WriteStartRow();
foreach (object o in row.ItemArray)
{
writer.WriteExcelAutoStyle dCell(o);
}
writer.WriteEndRow();
}
//write END OF REPORT at the end
//writer.WriteExcelAutoSty ledCell("E ND 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:Dat a>
</ss:Cell>
<ss:Cell ss:StyleID="General">
<ss:Data ss:Type="String">+0</ss:Da ta>
</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:Dat a>
</ss:Cell>
<ss:Cell ss:StyleID="General">
<ss:Data ss:Type="String">0</ss:Dat a>
</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>
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
----------------------
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.WriteExcelUnstyledC
writer.WriteEndRow();
//write data
foreach (DataRow row in data.Rows)
{
writer.WriteStartRow();
foreach (object o in row.ItemArray)
{
writer.WriteExcelAutoStyle
}
writer.WriteEndRow();
}
//write END OF REPORT at the end
//writer.WriteExcelAutoSty
//writer.Wr iteEndRow(); //commented out
// Close up the document
writer.WriteEndWorksheet()
writer.WriteEndDocument();
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:Dat
</ss:Cell>
<ss:Cell ss:StyleID="General">
<ss:Data ss:Type="String">+0</ss:Da
</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:Dat
</ss:Cell>
<ss:Cell ss:StyleID="General">
<ss:Data ss:Type="String">0</ss:Dat
</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>
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\UDLApplicati on\UDLAppl ication\Fo rm1.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.WriteExcelUnstyledC ell(col.Ca ption);
writer.WriteEndRow();
//write data
foreach (DataRow row in data.Rows)
{
writer.WriteStartRow();
foreach (object o in row.ItemArray)
{
writer.WriteExcelAutoStyle dCell(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.WriteExcelAutoStyle dCell("END OF REPORT"); // write first cell not blank
loopCounter += 1; // increase the counter
}
else
{
writer.WriteExcelAutoStyle dCell(""); // write a blank cells
}
}
writer.WriteEndRow(); // conclude the row
//write END OF REPORT at the end
//writer.WriteExcelAutoSty ledCell("E ND 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(myExceptio n.Message, "Excel Export", MessageBoxButtons.RetryCan cel, MessageBoxIcon.Asterisk);
}
}
}
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
//Write header row
writer.WriteStartRow();
foreach (DataColumn col in data.Columns)
writer.WriteExcelUnstyledC
writer.WriteEndRow();
//write data
foreach (DataRow row in data.Rows)
{
writer.WriteStartRow();
foreach (object o in row.ItemArray)
{
writer.WriteExcelAutoStyle
}
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.WriteExcelAutoStyle
loopCounter += 1; // increase the counter
}
else
{
writer.WriteExcelAutoStyle
}
}
writer.WriteEndRow(); // conclude the row
//write END OF REPORT at the end
//writer.WriteExcelAutoSty
//writer.WriteEndRow();
// Close up the document
writer.WriteEndWorksheet()
writer.WriteEndDocument();
writer.Close();
retry = DialogResult.Cancel;
}
}
catch (Exception myException)
{
//myException.Message;
retry = MessageBox.Show(myExceptio
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.WriteExcelAutoStyle
loopCounter += 1; // increase the counter
}
else
{
writer.WriteExcelAutoStyle
}
}
writer.WriteEndRow(); // conclude the row
Open in new window