Solved

Need to display data in an excel spreadsheet in IE from an xls template.

Posted on 2008-10-16
6
599 Views
Last Modified: 2013-11-26
How can i create an excel spreadsheet in an asp.net vb aspx.vb page?

I tried the following code and i just get a blank page....

*********************CODE***************************************************************

Imports System.Reflection
Imports System.Runtime.InteropServices
Imports Excel = Microsoft.Office.Interop.Excel

Partial Class Report_Forecasting_Excel_1
    Inherits System.Web.UI.Page

    'Create Excel Objects
    Dim thisExcel As Excel.Application
    Dim thisWorkbook As Excel.Workbook
    Dim thisWorksheet As Excel.Worksheet

    Private Sub CreateSpreadsheet()
        thisExcel = New Excel.Application
        thisExcel.Workbooks.Add("D:\INTERNET\test.JazzCameras.com\Test\Excel_Templates\Forecasting_Template_1.xls")
        thisWorkbook = thisExcel.Workbooks(1)
        thisWorkbook.Activate()
        thisWorksheet = thisWorkbook.Worksheets(1)
        thisWorkbook.Application.Visible = True
        thisWorksheet.Cells(10, 10) = "hello there"
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        CreateSpreadsheet()
    End Sub
End Class
0
Comment
Question by:LennyCXIII
  • 2
  • 2
  • 2
6 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 22738605
You are creating the spreadsheet but not returning it to the client. Use a streat to return the created spreadsheet to the client using the response.write method.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 22739695
having Office and use its automation on a web server is not really a good idea, not recommended. You should really use product like http://www.aspose.com/categories/file-format-components/aspose.cells-for-.net-and-java/default.aspx
0
 

Author Comment

by:LennyCXIII
ID: 22753052
CodeCruiser:  Can you be more specific in how to create the "stream"...

my need was to create an excel document that users can save as a formated excel file
with styles and printing preferences already applied...
seting pagebreaks and printing header rows on each page etc....
i worked arround this by making a table and changing the content type to excel in the aspx.vb code and putting the excel xml documentation in the <head></head> aspx code

this worked real well..

But i would still like to know how to create the stream as you mentioned.

and emoreau Why is it such a bad idea?




 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 

<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
 

<head id="Head1" runat="server">

<meta http-equiv="Content-Type" content="text/html; charset=us-ascii"/>

<meta name="ProgId" content="Excel.Sheet" />

<meta name="Generator" content="Microsoft Excel 11"/>
 

<title>Jazz Forecast By Product</title>

    <link href="JazzExcel_Styles.css" rel="stylesheet" type="text/css" />

    

 <xml>

 <o:DocumentProperties>

  <o:LastAuthor>Jazz Management Application</o:LastAuthor>

  <o:Version>2.0</o:Version>

 </o:DocumentProperties>

</xml>
 

<style>

<!--table

	{mso-displayed-decimal-separator:"\.";

	mso-displayed-thousand-separator:"\,";}

@page

	{mso-header-data:"&L&\0022Arial\,Bold\0022Jazz Confidential&C&D&RPage &P of &N";

	margin:0in .75in .5in .75in;

	mso-header-margin:.5in;

	mso-footer-margin:0in;

	mso-page-orientation:landscape;}

tr

	{mso-height-source:auto;}

br

	{mso-data-placement:same-cell;}

.style0

	{

	mso-protection:locked visible;

	mso-style-id:0;}

td

	{mso-style-parent:style0;

	padding-top:1px;

	padding-right:1px;

	padding-left:1px;

	mso-ignore:padding;

	mso-protection:locked visible;

	}

-->

</style>
 

<xml>

 <x:ExcelWorkbook>

  <x:ExcelWorksheets>

   <x:ExcelWorksheet>

    <x:Name>Test</x:Name>

    <x:WorksheetOptions>

     <x:Print>

      <x:ValidPrinterInfo/>

      <x:PaperSizeIndex>5</x:PaperSizeIndex>

      <x:HorizontalResolution>300</x:HorizontalResolution>

      <x:VerticalResolution>0</x:VerticalResolution>

     </x:Print>

     <x:PageBreakZoom>60</x:PageBreakZoom>

     <x:Selected/>

     <x:DoNotDisplayGridlines/>

     <x:ProtectContents>False</x:ProtectContents>

     <x:ProtectObjects>False</x:ProtectObjects>

     <x:ProtectScenarios>False</x:ProtectScenarios>

    </x:WorksheetOptions>

   </x:ExcelWorksheet>

  </x:ExcelWorksheets>

  <x:WindowHeight>13425</x:WindowHeight>

  <x:WindowWidth>24780</x:WindowWidth>

  <x:WindowTopX>240</x:WindowTopX>

  <x:WindowTopY>135</x:WindowTopY>

  <x:ProtectStructure>False</x:ProtectStructure>

  <x:ProtectWindows>False</x:ProtectWindows>

 </x:ExcelWorkbook>

 <x:ExcelName>

  <x:Name>Print_Titles</x:Name>

  <x:SheetIndex>1</x:SheetIndex>

  <x:Formula>='Test'!$1:$5</x:Formula>

 </x:ExcelName>

</xml>

    

</head>

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 69

Accepted Solution

by:
Éric Moreau earned 250 total points
ID: 22753167
>>and emoreau Why is it such a bad idea?

from http://support.microsoft.com/?kbid=257757:

Although such programmatic development can be implemented on a client system with relative ease, a number of complications can occur if Automation takes place from server-side code such as Microsoft Active Server Pages (ASP), ASP.NET, DCOM, or a Windows NT service.
0
 
LVL 83

Assisted Solution

by:CodeCruiser
CodeCruiser earned 250 total points
ID: 22753881
0
 

Author Closing Comment

by:LennyCXIII
ID: 31516056
both answers were right in thier own respect...
I found it easiest to set the excel params in the aspx file create the tables programatically in the aspx.vb and tell the browser its an excel file and let the user end figure the rest out and it works great I've had to impliment this at least 10 diff ways in the past two weeks... its quick and easy and alows me to set the printing properties like repeating table heading (top 2 cells) at each page. Thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

947 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

20 Experts available now in Live!

Get 1:1 Help Now