Automatically adding rows to excel file template - asp.net

I am running .net 4.0 asp.net app on IIS 7. I want to know if there is an easy way of adding rows to an excel 2007/2010 (not too fussed) template file that a user can download from the server.
For example, the user might check a couple of tick boxes and clicks a button on the web page. The server does a sql query on a table and comes back with the results. I have an excel file with some headings, column titles, formatting etc. on the web app's resources directory. I want to make a copy of this file, insert each of the results as a row into this file (insert first name into cell A3, last name into cell B3 etc.). And make it available for the user to save on their disk.
Since Excel (xls) format is an "Open format", I was wondering how easy/straight forward this would be. Is it a matter of loading the excel XML DOM and inserting XML elements? What are libraries I need to use?
Any help or resources would be greatly appreciated.

I did exactly this using an Excel template. You'll see how easy it is to do this using open source libraries as NPOI.
These links should help you get there:
Create Excel (.XLS and .XLSX) file from C#
Creating Excel spreadsheets .XLS and .XLSX in C#

Related

ASP.NET generated excel file has different format than extension

I have a web application made in old asp.net 2.0. I need to generate excel file with specific formated table (the best way if I can load the template and only put data in code behind). To do this I'm trying to use this library CarlosAg.ExcelXmlWriter:
http://www.carlosag.net/tools/excelxmlwriter/sample
And the final file is generated very well, but when I'm trying to open it in MS Excel warning popup appear with information that file has different format than the extension (xls). And also question if I want to open it if I trust the source of file. When I click yes, the file opened properly. On the other pc with different version of Excel, there's also warning, but different - file is broken and cannot be opened, the same in mobile (no possibility to open). What is interesting - I can open that file properly without any warnings in different office program, ex. WPS Office.
So I make two steps back and try to open just simple file generated by the sample of Carlos library and effect is the same. Is there any chance to "fix" the file in code behind so MS Excel could also open it properly without any warnings?
Dim book As Workbook = New Workbook()
Dim sheet As Worksheet = book.Worksheets.Add("Sample")
Dim row As WorksheetRow = sheet.Table.Rows.Add()
row.Cells.Add("Hello World")
book.Save("D:\Folder\test.xls")
You have to give the file extension xlsx.
The older xls is in fact the legacy (closed) binary format of office files.
However, since office 2007, Microsoft has adopted a open xml format for ALL office documents (well, ok, outlook pst, and MS-Access are exceptions). In fact, if you take any office document (word, power-point, excel) etc., try renaming the xlsx document as a zip file. You find that you can now open the zip archive, and you can see/look at and even modify the xml in that document.
(do try this rename as a zip file for fun and giggles - you see how you actually don't even need any 3rd party tools to open, play with, and mess with the content in the zip archive (so, all office documents are in fact a simple zip archive!!!).
So, newer office file formats are in fact xml markup - and you can modify it!!
Anyway, without a doubt that library you are using thus uses the above feature/fact of office documents now being xml formatted and NOT a propriety Microsoft binary format - you can in fact modify such documents now on ANY computer that is able to open a zip file, and modify text files.
As a result, you are giving the document name a legacy binary file name (xls), but opening a xml formatted file (plain text), and thus Excel is detecting this fact/issue. So, you need to give that document the open office xml format extension (xlsx).

Adding a custom template for extracting data in excel

I'm currently working on a project which one of its modules is to upload a custom excel file then my code should recognize the excel file's template.
then Users should be able to determine which data is put in which excel field.
I am writing this with asp.net mvc. and so far I'm screwed. anyone knows where should I start? or what library I should Use? I'd really appreciate the help
For Fetching Excel File data in c# , I recommend you to study about Aspose.Cells, It is a third party Dll, There API is Easy to understand and Very Easy to implement.
In that you can Fetch Your Excel Files as
Workbook - Full Excel File
Worksheets - Sheets Inside Excel File
Cells - Group of Excel Cell
Cell - A single Cell which you can access in the form of 2D Array like A1 Cell of Excel Sheet Will be Cell[0][0];
It also gives you facility to put formulaes, Sorting, Blank Row Deletion and many more Things
Here you can easily Export your Excel sheet in a DataTable and vice Versa
Have a look at it is Easier to use.Thanks
https://github.com/closedxml/closedxml/wiki/Showcase
this library was close to what I needed.

download data in excel from database without GridView

I Have to fetch data from database and download in Excel format without DataBind() on any Control.
Is There any Possible way to accomplish this task?
If you need to download in xlsx format, I can recommend ClosedXml - none of the nightmare of interop and requiring Excel on the same machine. Quite easy too - see this example: Adding DatatTable to Worksheet (ADO.net).
See also this example for delivering an Excel file with ASP.net

Generate Word (docx) Files with from Templates (dotx) on Server Application

I'm currently working on a project where I have to transfer an existing VB program into a Server Application using ASP.NET.
While I had success doing that there's one thing that I'm struggeling with:
The VB Program was using Microsoft Word Interop to generate Excel files and fill Word Templates with data. While i managed to be able to generate the files locally with Interop I can't get it to work for somebody that is accessing the Application from a client.
I also tried using OpenXML to solve my problem but somehow it always said that the file is corrupt after I tried to fill the bookmarks.
In the end the user shoud be able to download the Word document filled with the necessary data.
What would be the best solution for this problem?
If you have mostly the same document structure, you can prepare the whole document in Word. Set placeholders in the document like {Placeholder1} {Placeholder2}, parse the XML and Replace the Placeholders with your text, so you must not generate the whole document structure. Only Replace the Placeholders with your text.

export to excel file from asp.net

I have an ASP.NET 3.5 site that needs to export data to a pre-formatted Excel sheet. I am not allowed to modify the excel sheet in any way, just drop data into specific cells.
My question is, What is the simplest way to do this from an ASP.NET site. The user will always have Excel on their machine.
Is a 3rd party control my best option or is this a relatively simple task using VB.NET ?
dear use ExcelPackage. It rocks!!! I've used it in my project. It can take a preformatted excel file and use it as template. It is also very easy to use.
Use NPOI (http://npoi.codeplex.com/) if you want Excel 2000-2003 compatibility, or ExcelPackage (http://excelpackage.codeplex.com/) if you're only targeting Excel 2007/2008.
I've also rolled my own XML output in the older SpreadsheetML format (Excel 2002/2003), it was MUCH easier than the newer OOXML (Excel 2007/2008) format.

Resources