Hi all members,
I need your urgent help on this as im running out of time, I just understood that microsoft.office.interop.excel is not reccomended to be used on server-side application as exporting data using Microsoft.Office.Interop.Excel is avoided, since it needs application installation on server, is poorly scalable, and also when I tried after deployment I receive run time errors. I understand that OpenXML is great for server application and DOES NOT need excel to be installed to work. Thus can I know how should I change my codes from microsoft.office.interop.excel to OpenXML SDK?
My current codes actually allows me to export data of 2 gridviews into individual worksheets(1 gridview data in 1 individual worksheet) which I can set the name of each worksheet itself. My codes should also allow it to save into a specified folder in the folder and another pop up save dialogue to allow users to choose to OPEN/SAVE AS (their own desired location). Thus in each export button clicked there should be 2 excel files generated.
The below is my current codes:
protected void EXPORT_BUTTON_Click(object sender, EventArgs e) { Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application(); // creating new WorkBook within Excel application Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing); String DATA1 = "DATA1"; String DATA2 = "DATA2"; ExportToExcel(app, workbook, DATA_1, DATA1); workbook.Worksheets["Sheet1"].Delete(); workbook.Worksheets["Sheet2"].Delete(); workbook.Worksheets["Sheet3"].Delete(); ExportToExcel(app, workbook, DATA_2, DATA2); string FolderPath = ServerName + DirectoryLocation + DirectoryFolder + ExportsFolder; var filename = @"EXCEL_" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".xlsx"; workbook.SaveAs(FolderPath + filename, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing); workbook.Close(); app.Quit(); Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AddHeader("content-disposition", "attachment; filename=" + filename + ";"); Response.TransmitFile(FolderPath + filename); Response.Flush(); Response.End(); } public void ExportToExcel(Microsoft.Office.Interop.Excel._Application app, Microsoft.Office.Interop.Excel._Workbook workbook, GridView gridview, string SheetName) { // see the excel sheet behind the program app.Visible = false; Excel.Worksheet worksheet = (Excel.Worksheet)workbook.Worksheets.Add(); // changing the name of active sheet worksheet.Name = SheetName; // storing header part in Excel for (int i = 1; i < gridview.Columns.Count + 1; i++) { worksheet.Cells[1, i] = gridview.Columns[i - 1].HeaderText; } // storing Each row and column value to excel sheet for (int i = 0; i < gridview.Rows.Count - 1; i++) { for (int j = 0; j < gridview.Columns.Count; j++) { worksheet.Cells[i + 2, j + 1] = gridview.Rows[i].Cells[j].Text.ToString(); } } }
Your kind help is appreciated. thanks.