<a href="@Url.Action("Exporttoexcel", "Excelgenerator")">Export Excel</a>
var rang_currencyprice = worksheet.get_Range("C2", "C16");
rang_currencyprice.NumberFormat = "$* #,##0.00";
public ActionResult Exporttoexcel()
{
Itemmodel itm = new Itemmodel();
try
{
Excel.Application application = new Excel.Application();
Excel.Workbook workbook = application.Workbooks.Add(System.Reflection.Missing.Value);
Excel.Worksheet worksheet = workbook.ActiveSheet;
worksheet.Cells[1, 1] = "Itemlookup";
worksheet.Cells[1, 2] = "Description";
worksheet.Cells[1, 3] = "Price";
worksheet.Cells[1, 4] = "Cost";
int row = 2;
foreach(var it in itm.Findall())
{
worksheet.Cells[row, 1] = it.Itemlookup;
worksheet.Cells[row, 2] = it.Description;
worksheet.Cells[row, 3] = it.Price;
worksheet.Cells[row, 4] = it.Cost;
row++;
}
Formatexcel(worksheet);
workbook.SaveAs("d:\\test\\Item_list.xlsx");
workbook.Close();
Marshal.ReleaseComObject(workbook);
application.Quit();
Marshal.FinalReleaseComObject(application);
}
catch(Exception ex)
{
ViewBag.message = ex.Message;
}
return RedirectToAction("Index");
}
public void Formatexcel(Excel.Worksheet worksheet)
{
//Format Cells in loop
worksheet.get_Range("A1", "D1").EntireColumn.AutoFit();
//Format Heading
var range_heading = worksheet.get_Range("A1", "D1");
range_heading.Font.Bold = true;
range_heading.Font.Color = Color.Red;
range_heading.Font.Size = 13;
//Format Currency
//column price
var rang_currencyprice = worksheet.get_Range("C2", "C16");
rang_currencyprice.NumberFormat = "$* #,##0.00";
//column cost
var rang_currencycost = worksheet.get_Range("D2", "D16");
rang_currencycost.NumberFormat = "$* #,##0.00";
//Format Date
//var range_date = worksheet.get_Range("A1", "D1");
//range_date.NumberFormat = "mm/dd/yyyy";
}
Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.
”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.
Our community of experts have been thoroughly vetted for their expertise and industry experience.