Simple Library to Create Excel Worksheets and Convert to PDF in C# And ASP.Net

Introduction

After I knew NPOI from friends the last time, I also tried to find some other free open-source libraries to operate Excel worksheets in C#. During the summer holidays, I spent some time to get experience with these libraries a bit. Generally speaking, the functions are very well for operating Excel files in C# and to support operation of multi-workbook and support most of the Microsoft Excel elements, such as create Excel worksheets with dropdown list, formula, hyperlinks, Image and others. This article I will post a project used NPOI and Spire.XLS DLLs. With these DLLs, you can quickly write and manipulate Excel files and there is no need to install Microsoft Office on either of the development nor target systems, but it still needs .NET Framework 2.0 or above.

This project is for .NET framework 4.0.

Application Overview

First, I use NPOI to create an empty Excel worksheet and then insert an image and some hyperlinks into the sheet, then save the Excel file. Secondly, I use Spire.XLS to convert the Excel into PDF file.

Step 1: Use NPOI to create an Excel worksheet with image and hyperlinks. Here are the codes. NPOI (https://npoi.codeplex.com/) is an open source project that can help you read/write xls files, documents and PPT files. You can get the samples easily once you download the examples.

  1. HSSFWorkbook hssfworkbook = new HSSFWorkbook();  
  2. //create a new instance of DocumentSummaryInformation  
  3. DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();  
  4. dsi.Company = "NPOI";  
  5. hssfworkbook.DocumentSummaryInformation = dsi;  
  6. // create a new instance of SummaryInformation  
  7. SummaryInformation si = PropertySetFactory.CreateSummaryInformation();  
  8. si.Subject = "Excel Example";  
  9. hssfworkbook.SummaryInformation = si;  
  10. //here, we must insert at least one sheet to the workbook. Otherwise, Excel will say 'data lost in file'  
  11. ISheet sheet1 = hssfworkbook.CreateSheet("sheet1");  
  12.  ((HSSFSheet)hssfworkbook.GetSheetAt(0)).AlternativeFormula = false;  
  13.  ((HSSFSheet)hssfworkbook.GetSheetAt(0)).AlternativeExpression = false;  
  14. HSSFPatriarch patriarch = (HSSFPatriarch)sheet1.CreateDrawingPatriarch();  
  15. //create the anchor  
  16. HSSFClientAnchor anchor;  
  17. anchor = new HSSFClientAnchor(600, 255, 0, 0, 2, 2, 4, 7);  
  18. anchor.AnchorType = 0;  
  19.   
  20. //load an picture and insert it to the Excel sheet  
  21. HSSFPicture picture = (HSSFPicture)patriarch.CreatePicture(anchor, LoadImage("image.jpg", hssfworkbook));  
  22.   
  23. //insert a hyperlink  
  24. ICellStyle hlink_style = hssfworkbook.CreateCellStyle();  
  25. IFont hlink_font = hssfworkbook.CreateFont();  
  26. hlink_font.Underline = NPOI.SS.UserModel.FontUnderlineType.Single;  
  27. hlink_font.Color = HSSFColor.Blue.Index;  
  28. hlink_style.SetFont(hlink_font);  
  29. ICell cell;  
  30. //URL  
  31. cell = sheet1.CreateRow(0).CreateCell(0);  
  32. cell.SetCellValue("URL Link");  
  33. HSSFHyperlink link = new HSSFHyperlink(HyperlinkType.Url);  
  34. link.Address = ("http://google.com/");  
  35. cell.Hyperlink = (link);  
  36. cell.CellStyle = (hlink_style);  
  37. //link to a file in the current directory  
  38. cell = sheet1.CreateRow(1).CreateCell(0);  
  39. cell.SetCellValue("File Link");  
  40. link = new HSSFHyperlink(HyperlinkType.File);  
  41. link.Address = ("link1.xls");  
  42. cell.Hyperlink = (link);  
  43. cell.CellStyle = (hlink_style);  
  44. //e-mail link  
  45. cell = sheet1.CreateRow(2).CreateCell(0);  
  46. cell.SetCellValue("Email Link");  
  47. link = new HSSFHyperlink(HyperlinkType.Email);  
  48. //note, if subject contains white spaces, make sure they are url-encoded  
  49. link.Address = ("mailto:sales@e-iceblue.com?subject=Hyperlinks");  
  50. cell.Hyperlink = (link);  
  51. cell.CellStyle = (hlink_style);  
  52. //Write the stream data of workbook to the root directory  
  53. string filename = "test.xls";  
  54. FileStream file = new FileStream(filename, FileMode.Create);  
  55. hssfworkbook.Write(file);  
  56. file.Close(); 

Screenshot of the created Excel file:



Step 2: Since I need to convert an Excel file to PDF and NPOI only supports saving Excel as HTML, I searched and finally found the free Spire.XLS (https://freenetexcel.codeplex.com/). Besides converting an Excel file into PDF, it also supports converting an Excel worksheet to an Image, XML, CSV, TEXT and others. It is easily integrated with other third-party components. It is easy and only three lines of codes are needed.

  1. Workbook workbook = new Workbook();  
  2. workbook.LoadFromFile(filename);  
  3. workbook.SaveToFile("result.pdf",FileFormat.PDF); 

It says Spire.XLS supports many elements in an Excel file that can be converted to PDF successfully, such as chart, shape, smart art and and so on. Check the effected Screenshot of the resulting PDF file.



Summary

NPOI is open-source to easily read and write Excel 97-0, but it does not support Excel 2007, Excel 2010 and Excel 2013. Free Spire.XLS supports these versions of Excel files, but it is limited to 5 sheets per workbook and 150 rows per sheet. For personal use, both of them are enough. If this article helps, you can share it with your friends.