Generating Excel file from HTML - c#

I'm aware that I can generate an HTML <table> and save it as an .xls file to read into Excel, and that works fine and all, but it only gives me one sheet.
Is there a way to generate HTML so that I can have multiple sheets in a single .xls file? I've tried to simply generate more then one <table>, but they just end up getting appended to eachother.

Short Answer: No.
Longer Answer: You cannot cause an HTML generated page to split into multiple worksheets in an Excel file. Further, the HTML you generate for even a single page could cause Excel to choke on certain machines as it does the conversion when the file is loaded. We've seen a number of low powered machines take upwards of 5 minutes to show a HTML file in excel (simple table with rows/columns, nothing fancy) depending on size.
Better Answer: Use a third party product like ClosedXML or FileHelpers to generate a proper xlsx file.

there seems to be way though I didn't try it, see http://www.c-sharpcorner.com/UploadFile/kaushikborah28/79Nick08302007171404PM/79Nick.aspx and check the Worksheets attribute
check the official documentation at http://msdn.microsoft.com/en-us/library/Aa155477%28office.10%29.aspx
After installing the Help file you can find an example of a file with 3 Worksheets in XML Reference / Excel Workbook...

You can use open source ClosedXML, а wrapper around OpenXML to conveniently generate xlsx files - i.e. Office 2007+ format Excel files.

Related

How to identify an excel file(xls, xlsx, csv) which has been converted from .DLL file in c# dot net core

Basically need to identify the excel is originally created from a valid excel. I have tried using Microsoft.Interop.Excel File format.
Seems like I cannot use this without Microsoft office being installed. So need another approach for this problem.
EDIT :
Basically I want to be able to distinguish a valid excel from invalid one like an excel file which is converted from DLL. Because the file extension will say it is xls. If you try to open that file, it will open an empty workbook without any sheets. But I cannot decide an excel with no sheets is an invalid one.
If you want to idenify wheter file is a valid excel file or not, you can use 'trid file identifier'. TrID - File Identifier.
You can do it own your own also by reading magic number or file signaure bytes. Please go through this question Original file bytes from StreamReader, magic number detection.

How to properly programmatically convert a XLSX file to HTML using C#?

At work, we're modifying an XLSX file, and we would like to turn this modified file into an HTML file (to convert it into PDF using Puppeteer#, but it's not the point here).
We know how to get XML files of this XLSX, and I already found XSLCompiledTransform to convert XML files to HTML.
The annoyance here is that, from what I have read for XSLCompiledTransform, to transform XML file to HTML you need one stylesheet + one XML file.
This brings three problems :
It looks like the stylesheet into XLSX for each sheet isn't well formated to use with this XSLCompiledTransform.
The XLSX file contains multiples sheets, so we would have to fuse them in some manner, and we don't know how.
It is not just some random XML files, they're parts of an XLSX file. Thus there are also some XML files in addition to the sheets (like a workbook and other files) and we can't figure how we could generate an HTML file which is precisely like the XLSX file as open using Excel without using these XML files.
These problems could be resumed as: We struggle to find how to generate an HTML file which will look exactly like the original whole XLSX file.
We don't really want to create an HTML file from some XML files, so any means to transform an XLSX to HTML is good.
We also know that there are some tools and libs available to directly do this, but all the ones I've found aren't free, and we would like to avoid to pay for that as it's the first time we need it and maybe the last.
Does anyone know an accurate option to programmatically transform an XLSX file to HTML, keeping every style options and using C#?

Reading non-standard excel file with C#

What do i mean by 'non-standard'?
Take a look at these images: http://imgur.com/a/tFqHQ
The first one is the non-standard excel file. I'm pretty sure it's not an excel file, but the file's extension is .xls and for some reason Excel can open it, and understand it's structure.
The second image is the same file after it was opened in excel, and saved out to .xls (97-2003).
If excel can open it, and view it correctly, i should be able to do as well. Any tips how to approach this?
I have to mention that, my app have to use and read the non-standard excel files, because otherwise the user have to open the files one-by-one in (excel/libre office) and save it out in a correct format, which i would like to avoid for convenience.

Strip Excel file of Macros with C#

I've been asked to strip an Excel file of macros, leaving only the data. I've been asked to do this by converting the Excel file to XML and then reading that file back into Excel using C#. This seems a bit inefficient to me and I was thinking that it would be easier to simply load the source Excel file into C# and then create a new target Excel file and add the sheets from the source back into the target.
I don't know where macros live inside an Excel file, so I'm not sure if this would accomplish the task or not. So, will this work? Will simply copying the sheets from one file to another strip it of it's macros or are they actually stored at the worksheet level?
As always, any and all suggestions are welcome, including alternate suggestions or even "why are you even doing this???". :)
To do this programmatically, you can use the ZipFile class from the System.IO.Compression library in .NET from C#. (.NET Framework 4.5)
Rename the file to add a ".zip" extension, and then open the file as a ZIP archive. Look for an element in the resultant "xl" folder called "vbproject.bin", and delete it. Remove the .zip extension. Macros gone.
Your best bet is to save the workbook as an xlsx, close it, open it, then save as a format of your choice.
This will strip the macros and is robust. It will also work if the VBA is locked for viewing.
Closing and reopening the workbook is necessary otherwise the macros are retained.
If you're needing to use C# to do this, I agree that it would be easier to load the source Excel file into C# and create a new target file only copying over the cells and sheets you need. Especially if you're doing this for a large amount of excel files I would recommend just creating a small console app that, when given an excel sheet, will automatically generate a new excel sheet with just the data for you.
One tool that I've found extremely useful and easy to use for such tasks is EPPlus.

Merging two excel files in c# without using interop

I have to merge two excel files containing one sheet in each of them and I have to generate a third file containing two sheets corresponding to the two original sheets.
This task can be done using "interop" and the code works but when the same code is run in a system that does not contain MS Office, the process fails and an error comes up.
Can you please guide me as to what dll files to be included or how this merging could be done without using interop?
Thanks in advance.
From what I've experienced, there is unfortunately no framework way of doing this (without writing your own excel file reader). I happened across this interesting library which does just that.
http://exceldatareader.codeplex.com/
So far it has worked for our needs and requires no interop.
You should use an external component to work with excel files. I use the syncfusion xslIo.
If you only have raw data (no formulas, etc) you could also just save the files using the XML Spreadsheet 2003 (*.xml) format (its very easy to read) and process the data using standard XML tools.

Categories