Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

adding tabs to spreadsheet dynamically asp.net C#

Posted on 2009-05-19
7
588 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 Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

790 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