Solved

adding tabs to spreadsheet dynamically asp.net C#

Posted on 2009-05-19
7
587 Views
Last Modified: 2013-11-26
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
Comment
Question by:pemberley1210
  • 3
  • 2
  • 2
7 Comments
 
LVL 12

Accepted Solution

by:
GuitarRich earned 500 total points
ID: 24422961
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
 

Author Comment

by:pemberley1210
ID: 24423090
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
 

Author Comment

by:pemberley1210
ID: 24423108
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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 7

Expert Comment

by:zwei
ID: 24423180
You might need to use the Office Libraries in order to export the data into different sheets.

Excel.Worksheet
0
 
LVL 12

Expert Comment

by:GuitarRich
ID: 24423214
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
 
LVL 7

Expert Comment

by:zwei
ID: 24423230
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
 

Author Comment

by:pemberley1210
ID: 24423231
Yes - I'm running it on my local machine.  And I have Excel 2003.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question