Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using VS2008,ASP.net,C#.net,NPOI 1.2.3 dll.

I have a web applicaion which uses NPOI dll to export to excel 2003. How do I display a number with thousand separator in Indian style( 12, 12,34,567.89)in the exported excel sheet using program? The built in formula sum() should be applied on these exported cells with number format.

thanks in advance
george n t

The code is mentioned below.

protected void btnGenerateReport_Click(object sender, EventArgs e) { // Get the data to report on //var userAccounts = Membership.GetAllUsers();

// Create a new workbook and a sheet named "User Accounts"
HSSFWorkbook workbook = new HSSFWorkbook();
Sheet sheet = workbook.CreateSheet("User Accounts");
//var sheet = workbook.CreateSheet("User Accounts");

// Add header labels
int rowIndex = 0;
Row row = sheet.CreateRow(rowIndex);
row.CreateCell(0).SetCellValue("Username");
row.CreateCell(1).SetCellValue("Email");
row.CreateCell(2).SetCellValue("Joined");
row.CreateCell(3).SetCellValue("Last Login");
row.CreateCell(4).SetCellValue("Approved?");
row.CreateCell(5).SetCellValue("Qty");
rowIndex++;



//NUMBER FORMAT BEGIN
CellStyle detailCurrencySubtotalCellStyle = workbook.CreateCellStyle();
detailCurrencySubtotalCellStyle.BorderTop = CellBorderType.THIN;
detailCurrencySubtotalCellStyle.BorderBottom = CellBorderType.THIN;
Font detailCurrencySubtotalFont = workbook.CreateFont();
detailCurrencySubtotalFont.Boldweight = (short)FontBoldWeight.BOLD;
detailCurrencySubtotalCellStyle.SetFont(detailCurrencySubtotalFont);
detailCurrencySubtotalCellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("##,##,##,##0.00");

//NUMBER FORMAT END

row = sheet.CreateRow(rowIndex);
row.CreateCell(0).SetCellValue("preejo");
row.CreateCell(1).SetCellValue("preejo@gmail.com");
row.CreateCell(2).SetCellValue("01/Jan/2014");
row.CreateCell(3).SetCellValue("27/Jan/2014");
row.CreateCell(4).SetCellValue("true");
//row.CreateCell(5).SetCellValue(((121234567.89).ToString("N", new CultureInfo("hi-IN"))));
//row.CreateCell(5).SetCellValue(Convert.ToDouble(123456.78));
row.CreateCell(5).SetCellValue(123456.78);
//row.GetCell(5).CellStyle = cellStyle;
row.GetCell(5).CellStyle = detailCurrencySubtotalCellStyle;




rowIndex++;
row = sheet.CreateRow(rowIndex);
row.CreateCell(0).SetCellValue("joby");
row.CreateCell(1).SetCellValue("joby@gmail.com");
row.CreateCell(2).SetCellValue("01/Jan/2013");
row.CreateCell(3).SetCellValue("27/Jan/2013");
row.CreateCell(4).SetCellValue("false");
row.CreateCell(5).SetCellValue(Convert.ToDouble(2323313.43));
//row.GetCell(5).CellStyle = cellStyle;
row.GetCell(5).CellStyle = detailCurrencySubtotalCellStyle;

//}



// Auto-size each column
for (int i = 0; i < sheet.GetRow(0).LastCellNum; i++)
sheet.AutoSizeColumn(i);


// Add row indicating date/time report was generated...
sheet.CreateRow(rowIndex + 1).CreateCell(0).SetCellValue("Report generated on " + DateTime.Now.ToString());

//formula
rowIndex++;
row = sheet.CreateRow(rowIndex);
Cell cell = row.CreateCell(5);

cell.SetCellType(CellType.FORMULA);
cell.CellFormula = string.Format("SUM(F{0}:F{1})", 2, 3);


cell.CellStyle = detailCurrencySubtotalCellStyle;



// Save the Excel spreadsheet to a MemoryStream and return it to the client
using (MemoryStream exportData = new MemoryStream())
{
workbook.Write(exportData);

string saveAsFileName = string.Format("MembershipExport-{0:d}.xls", DateTime.Now).Replace("/", "-");

Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}", saveAsFileName));
Response.Clear();
Response.BinaryWrite(exportData.GetBuffer());
Response.End();
}
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900