Save as Html-Gridlines

When I try to save excel file as HTML using following code I don’t get excel like grid lines in webpage that gets saved. How can I achieve that Please help.


To explain more you see gridlines around cells in excel files- i want to see same gridlines in html page that I am saving.

string path = System.AppDomain.CurrentDomain.RelativeSearchPath;
string excelFilePath = System.AppDomain.CurrentDomain.BaseDirectory;

Workbook workbook = new Workbook();
workbook.Worksheets.RemoveAt(0);
Worksheet worksheet;

string sheetName = “StarawmanPDP”;
worksheet = workbook.Worksheets[workbook.Worksheets.Add()];

Cell cell = worksheet.Cells[“A1”];
cell.PutValue(“Hello World!”);

worksheet.Cells.StandardHeight = 15;
worksheet.Cells.StandardWidth = 25;
worksheet.IsGridlinesVisible = true;

worksheet.Name = sheetName;

workbook.Save(excelFilePath + @“Files” + Environment.UserName.ToString().ToUpper() + “.html”, SaveFormat.Html);

Hi Anuj,


Thank you for considering Aspose APIs, and welcome to Aspose.Cells support forum.

First of all, please note that Aspose.Cells APIs follow Excel’s guidelines and recommendations in its implementation. If you convert any spreadsheet to HTML format using Excel application, you will notice that the Excel application does not render the gridlines in the resultant HTML. This the reason that Aspose.Cells APIs do not provide any means to render the gridlines while converting spreadsheet to HTML format.

That being said, you can still achieve your requirements using the Style object exposed by Aspose.Cells APIs to simulate the gridlines in the resultant HTML. The trick is to set the borders for all the cells in your worksheet before rendering it to the HTML format. Please check the following piece of code, and give it a try on your side.

C#

Workbook workbook = new Workbook();
workbook.Worksheets.RemoveAt(0);
Worksheet worksheet;

string sheetName = “StarawmanPDP”;
worksheet = workbook.Worksheets[workbook.Worksheets.Add()];

Cell cell = worksheet.Cells[“A1”];
cell.PutValue(“Hello World!”);
cell = worksheet.Cells[“B1”];
cell.PutValue(“Hello Aspose!”);

worksheet.Cells.StandardHeight = 15;
worksheet.Cells.StandardWidth = 25;
worksheet.IsGridlinesVisible = true;

worksheet.Name = sheetName;

//Creating a range of cells containing data
Range range = worksheet.Cells.CreateRange(0, 0, worksheet.Cells.MaxDataRow + 1, worksheet.Cells.MaxDataColumn + 1);
//Creating a Style object to apply the borders to all cells
Style style = new Style();
//Setting border for all sides of a cell
style.SetBorder(BorderType.LeftBorder, CellBorderType.Thin, Color.Black);
style.SetBorder(BorderType.RightBorder, CellBorderType.Thin, Color.Black);
style.SetBorder(BorderType.TopBorder, CellBorderType.Thin, Color.Black);
style.SetBorder(BorderType.BottomBorder, CellBorderType.Thin, Color.Black);
//Applying the style to the range of cells
range.ApplyStyle(style, new StyleFlag() { Borders = true });

workbook.Save(myDir + “output.html”, SaveFormat.Html);

Please feel free to contact us back in case you need our further assistance with Aspose APIs.