Solved

adding tabs to spreadsheet dynamically asp.net C#

Posted on 2009-05-19
7
585 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

746 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now