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

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

Posted on 2008-10-16
6
624 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 70

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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
LVL 70

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

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.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

828 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