views:

2745

answers:

10

I was recently put in charge of updating a VB6 data collection app, to add the ability to generate Excel reports and print them through the app (both have to be done on same computer). Normally this wouldn't be an issue, I've generated Excel reports with VB6 before using the Excel Object.

So I went ahead and coded together the changes and presented them, and then I was told that the machine on which this program will be isntalled won't have a full copy of Excel on it, and I must come up with another solution.

I tried creating delimited files (comma seperated or text), but when these files are opened with excel they do not format nicely i.e. if one cell has 20 characters, half the chracters will be cut off due to the fixed cell size.

I have a couple more ideas:

1) I know openoffice has an api. Can this api be used to generate a properly formatted excel file? Is there a COM object I can use?

2)I found this tool: http://www.carlosag.net/Tools/ExcelXmlWriter/ However, it was written in VB.NET. Can I still use this tool in VB6?

I'm really stumped and don't know in which direction to head next. Anybody have any ideas regarding the utitiles above? Also, I am also open to any other suggestions/ better methods. Anything that would help me accomplish this task would be appreciated!

NOTE: The Excel version that will be used to view these reports is Excel 2007

+1  A: 

You can save excel files as html. In a web app I wrote that needed to export the data as excel, I just created an html file like the one excel exports. Then I served it as application/vnd.ms-excel. Excel opens it just fine. You could probably do something similar.

You can also find a library like the one you linked that writes the new office file format directly as I think they are derivatives of xml.

Will Rickards
+1. Excel is pretty good at opening any old HTML file - I think it's a feature for manual "screen scraping". So just create a vanilla HTML file, using tables where appropriate. Should work fine.
MarkJ
How would I open the file to print it? Wouldn't it have to be opened in a browser to get the html formatting? Is there a way to open (or embed) a browser with VB6, print the page, and close the browser? All this has to be done without prompts or alerts
darudude
You can control Internet Explorer through its COM object interface. Like this http://www.dailydoseofexcel.com/archives/2004/09/22/automating-internet-explorer/
MarkJ
I meant the following, no browser involved.Create an excel spreadsheet like you want. Save it as html through excel. Look at the html formatting. From your app produce similar html. Save it as a .html and then use Shell (built-in function) to open the file with Excel.
Will Rickards
darudude wants to print his reports on the machine without Excel. That's why I suggested using Internet Explorer to print the HTML
MarkJ
... or a better alternative would be to use the WebBrowser control to print the HTML from a VB6 app. Like this http://support.microsoft.com/kb/188271/
MarkJ
A: 

If you are willing to live with installing the .NET runtime you can wrap the library in option #2 with a .NET class exposed to COM. The class just passes calls to ExcelXMLwriter.

The alternative is to study the XML standard and write your own writer but that probably overkill for the project.

As for OpenOffice take a look here. Especially the VBA section of this page here. There appears to be a COM component you can use.

RS Conley
Wow never heard of this before. From what I'm reading, I need to install VB .NET and modify the dll to create an interface. I then need to generate a COM library to call the dll. Is all this correct?Can it be done in Visual studio 2005 express?
darudude
It can be done with command-line tools from the .NET SDK, so I would think VS Express can do it too.
Joel Mueller
Yes you are able to create Com exposed DLLs through VB Express. But you may have to goto the command line if you need to use the more advanced options.
RS Conley
+1  A: 

Create the CSV file as you would normally.

Let your desktop app do the formatting on the machine where XLS files are available, before it is printed.

shahkalpesh
+1 You could manipulate Excel in the usual way from an EXE on your machines with Excel. Or you could distribute an XLS with a macro.
MarkJ
+3  A: 

Let's take a look at these two requirements:

add the ability to generate Excel reports and print them through the app.

and

the machine on which this program will be isntalled[sic] won't have a full copy of Excel

Unless I misunderstand you, those seem to be mutually exclusive. If you mean you'll just be generating the reports on one machine and they'll be viewed and printed elsewhere, you might try using SpreadSheetML.

Be careful when googling for additional info on SpreadSheetML: there's a lot of misinformation out there that confuses SpreadSheetML with the new Xml format used for Excel in Office 2007. SpreadSheetML works as far back as OfficeXP, and even in a limited sense in Office 2000.

Joel Coehoorn
This seems like a fairly easy solution. Is there any easy way to generate the XML data? The only way I can think of right now would be essentialyl creating a text file with xml tags and saving it with an xml extension.
darudude
I don't know what xml support if any is built into vb6, but even if you have to treat as plain text it's likely still better than running through OpenOffice or calling out to .Net from vb6.
Joel Coehoorn
K I didn't read through your solution very well. "If you mean you'll just be generating the reports on one machine and they'll be viewed and printed elsewhere" - the machine that needs to generate the reports, also needs to print them. The reports are to be archived in an excel compatible format.
darudude
The MSXML COM object works just fine from VB6. Or at least it did many years ago when I was last forced to touch VB6.
Joel Mueller
I'm curious what the interface for this program is. On the one hand it sounds headless/server-class. On the other hand, printing from headless stations is a bad idea.
Joel Coehoorn
We are interfacing with some very very old PLCs (20+ years old). I have recently been asked to generate reports from the PLCs. Now these PLCs had the option to print to a serial dot matrix printer.
darudude
However, due to the lack of serial printers, I needed to create a solution that uses the tools we have avaiable today - computer + normal printer. I need to poll the serial port and then at certain times of the day, print a report through the printer attached on the pc
darudude
I also need to archive the reports, so they can be taken off the PC by USB and viewed elsewhere if nessessary (to create charts, etc). Thats why I was hoping to store the reports in an excel compatible format.
darudude
A: 

It is possible to create an Excel workbook on a machine without Excel installed** by using a Jet connection -- any Jet connection -- to executing a "make table query" SQL DML or CREATE TABLE SQL DLL where the table in question is an Excel one e.g. consider this VBA code:

Dim cat
Set cat = CreateObject("ADOX.Catalog")
With cat  

  ' Create a new Jet .mdb
  .Create _
      "Provider=Microsoft.Jet.OLEDB.4.0;" & _
      "Data Source=" & _
      Environ$("temp") & "\DropMe.mdb"
  ' Use .mdb's Jet connection to create an Excel table
  ' (will create a new workbook to contain it)
  .ActiveConnection.Execute _
     "CREATE TABLE" & _
     " [Excel 8.0;HDR=NO;Database=C:\db.xls;].[Sheet1]" & _
     " (col FLOAT);"
End With

Jet is a deprecated Windows component that is ever-present on pre-Vista Windows machines and remains a free download from MS via MDAC.

That said, "to print them through the [Excel] app" you clearly need the Excel app!

** I think it is possible: I can't find a machine without Excel on which to test!

onedaywhen
MDAC no longer includes Jet, it is a separate download. On supported machines (Win2K through Win7 Beta if you call that "supported") Jet 4.0 is part of the OS anyway.Yes, Jet can be used to create Excel workbooks but it can't format cells or columns.
Bob
MDAC 2.5 includes Jet and MDAC 2.5 can still be downloaded, that's what I meant :)
onedaywhen
A: 

Check this: http://support.microsoft.com/default.aspx?scid=kb;en-us;Q271572

Do the same and save the file with .xls extension instead.

Alternatively, you can create HTML Table in-momery content and write it into a file, with extension xls.

nils_gate
A: 

I found a solution:

I found a DLL that can be used to create and print Excel Spreadsheets. I think it is the same as the XML writer but for VB6.

darudude
A: 

Creating a SpreadsheetML file from VB6 should be very simple It's just a text file containing a particular flavor of xml. I belive you can get all the docs for SpreadsheetML and WordML from MSDN.

I currently produce around 60 different custom reports from a vb.net application using SpreadsheetML. There is at least one potential problem due to the changes in file extensions and document security implemented in Excel 2007. If you generate a SpreadsheetML based excel file and use the .xls extension it will open just fine in 2003. Excel 2007 will pop up a dialog informing you that the file extension does not match the file content. It will open and display the file correctly after the warning dialog. If you change the extension on the SpreadsheetML document to .xml, Excel 2007 will open it with no complaint. However your xml based spreadsheets may open in IE on machines running Excel 2003.

You are aware that there's a user "Ken White" here already, aren't you? I've been here a month or two already. You might want to use something else to avoid any confusion (or get the blame for something you didn't say).
Ken White
A: 

Can you post a link to the DLL you found?

Peter
A: 

I use NPOI It does not use excel library and gives output in excel 2003 format

Thunder