Access Macro Enabled Excel on Webpage using asp.net - asp.net

I need to access macro enable and external sourced excel 2003 calculator from asp.net web page. Excel accepts few values and returns some numbers post calculations.
Users are not allowed to amend/access the excel from office hence need web access to use the excel calculator features where user enter the values on webpage and pass to Excel and display results on webpage.
I tried to use sharepoint excel services due to macros's and external source on the excel file it fails excel services capability.
I would appreciate if anyone can point me in right direction

You can try EasyXLS Excel library. It can be integrated in ASP.NET web pages. It reads xls file macro enabled files and includes a calculator similar with the one from MS Excel.
// Create an instance of the class that read XLS files
ExcelDocument workbook = new ExcelDocument();
// Read XLS file
workbook.easy_LoadXLSFile("Excel macro enabled.xls");
// Get a sheet and compute the formulas
xlsWorksheet = (ExcelWorksheet) workbook .easy_getSheetAt(0);
String sError = xlsWorksheet.easy_computeFormulas(workbook, true);
// Check the result of the formula
String result = xlsWorksheet.easy_getExcelTable().easy_getCell(3,0).getFormulaResultValue();
To display the existing values and enter later other values you can use this code:
DataSet ds = workbook.easy_ReadExcelWorksheet_AsDataSet(xlsWorksheet);
and set the DataTable as data source for a GridView for example:
http://www.easyxls.com/manual/FAQ/import-excel-to-gridview.html
EasyXLS has also some open source code (more complex) for displaying an Excel file in ASP.NET pages that can be adjusted to your needs:
http://www.easyxls.com/excel-to-gridview

Related

Generating Excel Documents with ASP.NET Website

I have an ASP.NET application that helps the user create a Gridview with certain data in it. Once this table is generated I want the user to push a button and be able to save the table as an Excel document.There are two different methods I know of:
Using HtmlTextWriter with ContentType "application/vnd.ms-excel" to send the file as an HttpResponse. I use GridView1.RenderControl(htmlTextWriter) to render the gridview. This almost works, but the excel file always shows a warning when the file opens because the content doesn't match the extension. I have tried various content types to no avail. This makes sense I guess, because I'm using an HtmlWriter. It also doesn't seem a good practice.
The second thing I've tried is generating the Excel file using Office Automation. But for the file to be generated, I need to save it to disk and then read it again. From what I have read, this is the only way, because the Excel object only becomes a real Excel file once you save it. I found that the .saveas method from the Excel class would throw an exception because of write permissions, even if I tried to save in the App_Data folder. So I did some research and found that apparently Office Automation is discouraged for web services: https://support.microsoft.com/en-us/kb/257757
Microsoft does not currently recommend, and does not support,
Automation of Microsoft Office applications from any unattended,
non-interactive client application or component (including ASP,
ASP.NET, DCOM, and NT Services), because Office may exhibit unstable
behavior and/or deadlock when Office is run in this environment.
There surely must be a save way to have a website generate an Excel file and offer it to the user!? I can't imagine that this problem is unsolved or so rare that nobody cares about it, but yet I can't find any good solution to this.
the easiest (and best) way to create an excel file is by using epplus
Epplus sample for webapplication
using (ExcelPackage pck = new ExcelPackage())
{
ExcelWorksheet ws = pck.Workbook.Worksheets.Add("Demo");
//Load the datatable into the sheet, starting from cell A1. Print the column names on row 1
ws.Cells["A1"].LoadFromDataTable(tbl, true);
//Format the header for column 1-3
using (ExcelRange rng = ws.Cells["A1:C1"])
{
rng.Style.Font.Bold = true;
rng.Style.Fill.PatternType = ExcelFillStyle.Solid; //Set Pattern for the background to Solid
rng.Style.Fill.BackgroundColor.SetColor(Color.FromArgb(79, 129, 189)); //Set color to dark blue
rng.Style.Font.Color.SetColor(Color.White);
}
//Example how to Format Column 1 as numeric
using (ExcelRange col = ws.Cells[2, 1, 2 + tbl.Rows.Count, 1])
{
col.Style.Numberformat.Format = "#,##0.00";
col.Style.HorizontalAlignment = ExcelHorizontalAlignment.Right;
}
//Write it back to the client
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment; filename=ExcelDemo.xlsx");
Response.BinaryWrite(pck.GetAsByteArray());
}

How to send excel file directly to printer or print html code?

I have an ASP.Net 3.5 (C#) site that loads an excel file and then I want to print on a label printer at teh user's default printer?
Excel File is populated on sheet1
Print Area has been set in the file
When I print from excel to the label printer it prints fine.
I would like to send the excel file to the user's default printer. and have it automatically print the print area or the first sheet.
I would be open to sending a HTML String that has my data formatted in a table if that is possible. That would eliminate any need for the excel file.
Microsoft Excel is not installed on the webserver so using the Excel Refrence is out. But I do have the Microsoft Office SDK 2.0 and can use that if needed.
Thanks in advance for any tips or any directions.
Anything you could do manually, you can record using the macro recorder and figure out the code. Here is a generated code for sending files to my printer:
Application.ActivePrinter = "HP LaserJet P2014 on Ne03:"
ExecuteExcel4Macro "PRINT(1,,,1,,,,,,,,2,""HP LaserJet P2014 on Ne03:"",,TRUE,,FALSE)"
You could change the variables to match your own printer

Code to upload an excel file and read it in a dataset

I have a requirement , where in I have to upload an .xlsx file on the server and then read the file into a data table using c# , asp.net.
I need to get your file onto the server.
I need to open the (uploaded?) file.
I need to read the contents of the file, applying any nessesary data parsing.
I need to display the results in a gridview.
Help would be appreciated.

Export Excel object in ASP.NET

I am trying to export data to Excel from ASP.NET. The downloaded file is created on the fly when a link is clicked. The code that I use is something like this:
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
I am not exporting from a grid. I have a list of business objects that I want to loop through to export.
How do I have this file exported?
Thanks.
You really shouldn't be automating the Excel application in a server environment like ASP.NET. This MSKB article should be enough to scare you away from it: http://support.microsoft.com/kb/257757
OfficeWriter is a library for generating (and then streaming) Excel workbooks from ASP.NET that is intended for server use:
http://www.officewriter.com

Exporting a HTML Table to Excel from ASP.NET MVC

I am currently working with ASP.NET MVC and I have an action method that displays few reports in the view in table format.
I have a requirement to export the same table to an Excel document at the click of a button in the View.
How can this be achieved? How would you create your Action method for this?
In your controller action you could add this:
Response.AddHeader("Content-Disposition", "filename=thefilename.xls");
Response.ContentType = "application/vnd.ms-excel";
Then just send the user to the same view. That should work.
I'm using component, called Aspose.Cells (http://www.aspose.com/categories/.net-components/aspose.cells-for-.net/).
It's not free, though the most powerful solution I've tried +)
Also, for free solutions, see: Create Excel (.XLS and .XLSX) file from C#
Get data from database using your data access methods in dot net.
Use a loop to get each record.
Now add each record in a variable one by one like this.
Name,Email,Phone,Country
John,john#john.com,+12345,USA
Ali,ali#ali.com,+54321,UAE
Naveed,naveed#naveed.com,+09876,Pakistan
use 'new line' code at the end of each row (For example '\n')
Now write above data into a file with extension .csv (example data.csv)
Now open that file in EXCEL
:)

Resources