How to create Excel spreadsheet on the fly using C#?
In this code snippet, we will see how we can create Microsoft Excel spreadsheet / worksheet using C#. In this code we will generate the sheet on the fly. There are lot of freeware/opensources available to do this work but in our code snippet we will use spire.xls
Background In this code-snippet, we discussed about
We have the requirements to generate/create Excel Spreadsheet using dynamic data on the fly. There are lot of open sources and freewares available including ICEBlue, EPPS, Excelgenerator. In our example, we will use free spire.xls from ICEBlue to create/meet our requirements.
What we need to do?
No, any special activities required you just need to include a dll (Dynamic Lnk Library) in your project and you're ready to start the work. Here is the code, which will create spreadsheet, there are lot of feature, I am not going to discuss here.
using Spire.Xls;
namespace ExcelOnTheFly.ExceleHelper
{
public class SampleExcel
{
private string _xlsFilePath = HttpContext.Current.Server.MapPath("~/Sample/XLSFiles/");
private string _xlsFileName;
private Workbook workBook;
private Worksheet workSheet;
public SpireExcel()
: this("myExcel.xls")
{ }
public SampleExcel(string xlsFileName)
{
_xlsFileName = xlsFileName;
workBook = new Workbook();
}
public void CreateSampleExcel()
{
workSheet = workBook.Worksheets[0];
workSheet.Range["A1"].Text = "This is a sample Excel dcouemnt and created on the fly using C#";
workBook.SaveToFile(_xlsFilePath + _xlsFileName);
}
}
}
Conclusion
how we can create Microsoft Excel sheet or spreadsheet on the fly using dynamic data.
Copy/paste above code and enjoy the taste of this great tool.
Good explanation