There are several Frameworks for this solution, the one I used the most was EEPLus a DLL and does not need to be installed), but there are many others like:
As I said what I used the most and had a better experience was EEPLus .
See how easy it is to use it:
Create Tab
private static ExcelWorksheet CreateSheet(ExcelPackage p, string sheetName)
{
p.Workbook.Worksheets.Add(sheetName);
ExcelWorksheet ws = p.Workbook.Worksheets[1];
ws.Name = sheetName; //Setting Sheet's name
ws.Cells.Style.Font.Size = 11; //Default font size for whole sheet
ws.Cells.Style.Font.Name = "Calibri"; //Default Font name for whole sheet
return ws;
}
Join columns
//Merging cells and create a center heading for out table
ws.Cells[1, 1].Value = "Sample DataTable Export"; // Heading Name
ws.Cells[1, 1, 1, dt.Columns.Count].Merge = true; //Merge columns start and end range
ws.Cells[1, 1, 1, dt.Columns.Count].Style.Font.Bold = true; //Font should be bold
ws.Cells[1, 1, 1, dt.Columns.Count].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; // Aligmnet is center
Add stilo in the cell
//Setting the background color of header cells to Gray
var fill = cell.Style.Fill;
fill.PatternType = ExcelFillStyle.Solid;
fill.BackgroundColor.SetColor(Color.Gray);
Add border style
//Setting Top/left,right/bottom borders.
var border = cell.Style.Border;
border.Bottom.Style = border.Top.Style = border.Left.Style = border.Right.Style = ExcelBorderStyle.Thin;
Add formula
//Setting Sum Formula
cell.Formula = "Sum(" + ws.Cells[3, colIndex].Address + ":" + ws.Cells[rowIndex - 1, colIndex].Address + ")";
Add image
private static void AddImage(ExcelWorksheet ws, int columnIndex, int rowIndex, string filePath)
{
//How to Add a Image using EP Plus
Bitmap image = new Bitmap(filePath);
ExcelPicture picture = null;
if (image != null)
{
picture = ws.Drawings.AddPicture("pic" + rowIndex.ToString() + columnIndex.ToString(), image);
picture.From.Column = columnIndex;
picture.From.Row = rowIndex;
picture.From.ColumnOff = Pixel2MTU(2); //Two pixel space for better alignment
picture.From.RowOff = Pixel2MTU(2);//Two pixel space for better alignment
picture.SetSize(100, 100);
}
}
Convert to PDF
Workbook workbook = new Workbook();
//Load excel file
workbook.LoadFromFile(info.Name);
//Save excel file to pdf file.
workbook.SaveToFile("result.pdf", Spire.Xls.FileFormat.PDF);
Tutorial links:
>
link
link
link