• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 594
  • Last Modified:

adding tabs to spreadsheet dynamically asp.net C#

I'm using this code to create an excel spreadsheet at runtime.  Is there a way - using this method to insert more spreadsheets into the same workbook?  I've tried  using Microsoft.Office.Interop.Excel but cannot get it to work in VS 2005 without errors.  I've looked up the errors I receive from the above code and tried the solutions to no avail.

The code below, however works - I just can't figure out how to add additional worksheets.
Does anyone know how to accomplish this, using this method?

Thanks.
public void CreateSpreadsheet()
        {
            string name = "SupplementalQuestions.xls";
            HttpContext context = HttpContext.Current;
            context.Response.Clear();
           foreach (DataColumn column in table.Columns)
            {
                context.Response.Write(column.ColumnName + "\t");
            }
 
            context.Response.Write(Environment.NewLine);
            foreach (DataRow row in table.Rows)
            {
                for (int i = 0; i < table.Columns.Count; i++)
                {
                    context.Response.Write(row[i].ToString().Replace(";", string.Empty) + "\t");
                }
                context.Response.Write(Environment.NewLine);
               
            }
            context.Response.ContentType = "application/ms-excel";//"text/csv";
 
            context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name);
 
      
            context.Response.BufferOutput = true;
 
            context.Response.Flush();
 
            context.Response.Close();
 
              
 
 
        }

Open in new window

0
pemberley1210
Asked:
pemberley1210
  • 3
  • 2
  • 2
1 Solution
 
GuitarRichCommented:
effectively you are just creating a CSV/Tab delimeted file there so I don't think you would be able to add extra worksheets without using the Excel interop assemblies. What errors are you getting when using those?
If you want to create files that will only be used in Excel 2007 you can use ExcelPackage a .net assembley that will create documents in the OfficeOpenXML format - have a look at these examples.
These can also be opened in Excel 2003 - but you need a converter from Microsoft.
0
 
pemberley1210Author Commented:
Retrieving the COM class factory for component with CLSID {00020819-0000-0000-C000-000000000046} failed due to the following error: 80040154.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Runtime.InteropServices.COMException: Retrieving the COM class factory for component with CLSID {00020819-0000-0000-C000-000000000046} failed due to the following error: 80040154.

Source Error:


Line 853:        {
Line 854:            Microsoft.Office.Interop.Excel.Application oExcel = new Microsoft.Office.Interop.Excel.Application();
Line 855:            Microsoft.Office.Interop.Excel.Workbook oBook = new Microsoft.Office.Interop.Excel.Workbook();
Line 856:
Line 857:
 
0
 
pemberley1210Author Commented:
Stack Trace:


[COMException (0x80040154): Retrieving the COM class factory for component with CLSID {00020819-0000-0000-C000-000000000046} failed due to the following error: 80040154.]
   HRePartners.ApplicantSearch.GenerateExcel() in C:\Documents and Settings\jwinger\My Documents\Visual Studio 2005\Projects\HRePartners\ApplicantSearch.aspx.cs:855
   HRePartners.ApplicantSearch.CreateTable() in C:\Documents and Settings\jwinger\My Documents\Visual Studio 2005\Projects\HRePartners\ApplicantSearch.aspx.cs:752
   HRePartners.ApplicantSearch.LinkButton1_Click(Object sender, EventArgs e) in C:\Documents and Settings\jwinger\My Documents\Visual Studio 2005\Projects\HRePartners\ApplicantSearch.aspx.cs:653
   System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) +105
   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +107
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +174
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +5102

 


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
zweiSoftware DeveloperCommented:
You might need to use the Office Libraries in order to export the data into different sheets.

Excel.Worksheet
0
 
GuitarRichCommented:
This might be a silly question, and forgive me if it sounds patronising, not ment to - but is Excel installed on the server you are testing this from?
0
 
zweiSoftware DeveloperCommented:
Maybe the COM component needs to be registered?

Start + Run, regsvr32 C:\file.dll

Or maybe you are trying to run a 64-bit application, if so you'd have to force it to 32-bit (use Corflags).
0
 
pemberley1210Author Commented:
Yes - I'm running it on my local machine.  And I have Excel 2003.
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now