An XLSX file is a more complicated file type than an .xls. An .xlsx, as well as other office 2007+ formats like .docx, are actually zip files that contain multiple parts for the document. You would need a third-party control in order to create them properly.
Don't forget to mark useful responses as Answer if they helped you towards a solution.
If you want to create or edit Office document in ASP.NET or other server-side application, I suggest you consider using Open XML SDK which provide .NET managed class library for you to read, edit, create office documents. And the sdk also provides tools
for you to generate code from existing office document which is very helpful for creating prototype code.
The code you provided is very nice. However it is based on the traditional COM object model which is not recommended for server-side application (like ASP.NET web app, webservice app, etc...). Here is a KB article mentioned this:
Use a library that allows you to actually work with creating Excel documents that takes care of creating the file correctly. Check out something like the
Microsoft OpenXML SDK or
ExcelPackage.
Arun Allimut...
0 Points
1 Post
How to create a .xlsx in asp.net
Feb 23, 2013 06:41 PM|LINK
I'm able to create .xls. I unable to create .xlsx file using asp.net. could you help someone on the same.
thanks in advance.
markfitzme
Star
14319 Points
2215 Posts
Re: How to create a .xlsx in asp.net
Feb 23, 2013 07:18 PM|LINK
An XLSX file is a more complicated file type than an .xls. An .xlsx, as well as other office 2007+ formats like .docx, are actually zip files that contain multiple parts for the document. You would need a third-party control in order to create them properly.
ramiramilu
All-Star
95275 Points
14072 Posts
Re: How to create a .xlsx in asp.net
Feb 24, 2013 08:04 AM|LINK
in C#,, use Openxml SDK...
thanks,
JumpStart
GaurangNaik
Contributor
2066 Points
495 Posts
Re: How to create a .xlsx in asp.net
Mar 04, 2013 06:20 AM|LINK
Hi,
Yes you can create it. Use VSTO project. its a big topic. you can start from here.
http://msdn.microsoft.com/en-US/office/hh128771
GauranG
aaa_78600
Participant
820 Points
170 Posts
Re: How to create a .xlsx in asp.net
Mar 04, 2013 11:05 AM|LINK
private void GenerateExcelFile(DataTable _dataTable, string _saveFileLocation) { int indexRow = 0; int indexCol = 0; int totalRows = _dataTable.Rows.Count; int countColumns = _dataTable.Columns.Count; int startIndex = 0; String[,] objRecored = new String[totalRows, countColumns]; foreach (DataColumn col in _dataTable.Columns) objRecored[indexRow, indexCol++] = col.ToString(); indexCol = 0; indexRow += 1; for (startIndex = 0; startIndex < totalRows; startIndex++) { foreach (DataColumn col in _dataTable.Columns) objRecored[indexRow, indexCol++] = _dataTable.Rows[startIndex][col].ToString(); indexCol = 0; indexRow++; } try { Microsoft.Office.Interop.Excel._Application _Application = new Microsoft.Office.Interop.Excel.Application(); { Microsoft.Office.Interop.Excel._Workbook _Workbook = _Application.Workbooks.Add(Type.Missing); Microsoft.Office.Interop.Excel._Worksheet _Worksheet = (Excel._Worksheet)_Workbook.ActiveSheet; //-------------------------------------header Range----------------------- Microsoft.Office.Interop.Excel.Range headerRg_first = (Excel.Range)_Worksheet.Cells[1, 1]; Microsoft.Office.Interop.Excel.Range headerRg_last = (Excel.Range)_Worksheet.Rows.Cells[1, countColumns]; Microsoft.Office.Interop.Excel.Range headerRange = _Worksheet.get_Range(headerRg_first, headerRg_last); headerRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Silver); headerRange.EntireRow.Font.Bold = true; headerRange.Font.Name = "Verdana"; headerRange.Font.Size = "10"; headerRange.RowHeight = 22; headerRange.Borders.Color = System.Drawing.Color.Black; //------------------------------Record Range-------------------------------------------------------------------------- Microsoft.Office.Interop.Excel.Range RecoredRg_first = (Excel.Range)_Worksheet.Cells[1, 1]; Microsoft.Office.Interop.Excel.Range RecoredRg_last = (Excel.Range)_Worksheet.Cells[totalRows, countColumns]; Microsoft.Office.Interop.Excel.Range RecordRange = _Worksheet.get_Range(RecoredRg_first, RecoredRg_last); RecordRange.Value = objRecored; //RecordRange.HorizontalAlignment = Excel.XlHAlign.xlHAlignLeft; RecordRange.Borders.Color = System.Drawing.Color.Black; RecordRange.EntireColumn.AutoFit(); if (System.IO.File.Exists(_saveFileLocation)) System.IO.File.Delete(_saveFileLocation); _Workbook.SaveAs(_saveFileLocation, Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing); } _Application.Quit(); } catch (Exception ex) { } }Steven Cheng...
Contributor
4187 Points
547 Posts
Microsoft
Moderator
Re: How to create a .xlsx in asp.net
Mar 06, 2013 08:50 AM|LINK
Hi Arun,
If you want to create or edit Office document in ASP.NET or other server-side application, I suggest you consider using Open XML SDK which provide .NET managed class library for you to read, edit, create office documents. And the sdk also provides tools for you to generate code from existing office document which is very helpful for creating prototype code.
#Open XML SDK 2.0 for Microsoft Office
http://www.microsoft.com/en-us/download/details.aspx?id=5124\\
#An introduction to Open XML SDK 2.0 h
ttp://openxmldeveloper.org/blog/b/openxmldeveloper/archive/2009/06/02/4730.aspx
#Generate Word Documents Using Open XML SDK 2.0 for Microsoft Office
http://www.aspnetwiki.com/implementing-open-xml-sdk-2-0-for-microsoft-office
@aaa_78600,
The code you provided is very nice. However it is based on the traditional COM object model which is not recommended for server-side application (like ASP.NET web app, webservice app, etc...). Here is a KB article mentioned this:
#Considerations for server-side Automation of Office
http://support.microsoft.com/kb/257757?wa=wsignin1.0
Feedback to us
Microsoft One Code Framework
prabu.raveen...
Contributor
5020 Points
955 Posts
Re: How to create a .xlsx in asp.net
Mar 06, 2013 08:58 AM|LINK
Hi,
Use a library that allows you to actually work with creating Excel documents that takes care of creating the file correctly. Check out something like the Microsoft OpenXML SDK or ExcelPackage.
seysmail
Member
2 Points
1 Post
Re: How to create a .xlsx in asp.net
Apr 24, 2013 05:03 PM|LINK
Hi, can you please post the code for "get_range" as used in the