ASP MVC Create/Export Excel using NPOI

Pemrosesan excel menggunakan Excel.Interop di ASP sepertinya bermasalah.

1. Perlu meng-install excel di server

2. Ada potensi error

“Retrieving the COM class factory for component with CLSID {00024500-0000-0000-C000-000000000046} failed due to the following error: 8000401a The server process could not be started because the configured identity is incorrect. Check the username and password. (Exception from HRESULT: 0x8000401A).” http://stackoverflow.com/questions/9095688/asp-net-com-interop-excel-generation-issue-when-hosting

Alternatif library yang sedang saya coba adalah NPOI. Contoh kode untuk create excel pada NPOI

[code language=”csharp”]
public ActionResult DownloadManufacturerData()
{
Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); //supaya file tidak corrupt
//Thread.CurrentThread.CurrentCulture = CultureInfo.InstalledUICulture;

XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = (XSSFSheet)workbook.CreateSheet("manufacturer");

XSSFRow row = (XSSFRow)sheet.CreateRow((short)1);
XSSFCell cell = (XSSFCell)row.CreateCell(1);
cell.SetCellValue(123);

//write to file
MemoryStream ms = new MemoryStream();
workbook.Write(ms);

return File(ms.ToArray(), "application/vns.ms-excel", "manufacturer.xlsx");
}
[/code]

Referensi:

http://poi.apache.org/spreadsheet/quick-guide.html

Chandra Oemaryadi has written 244 articles

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>