Solved

adding tabs to spreadsheet dynamically asp.net C#

Posted on 2009-05-19
7
589 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
[X]
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
  • 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 our DevOps Teams Maximize Uptime

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us. Read the use case whitepaper.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

726 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