September 16, 2024 · 5 min · 861 words · vanillaiice
When I want to programmatically generate excel tables, I usually use the tealeg/xlsx package, since it does a great job of reading and writing xlsx files, and is very easy to use. However, this time, I was working on a big and complex project, and I found myself writing a lot of repeating code. Here is a dummy example:
Hmmm…as we can see, we have to manually add each row, and then add cells one by one, which is not cool. An easy (and smart) way to make the code shorter and concise would be to implement Sheet, Row and Cell types that have methods that do the same things as in the code block above. This is exactly what the table package does, it’s simply a wrapper around the xlsx package. Here are the types in question, from higher level to lower level:
// Table represents a table in a xlsx file.
typeTablestruct{file*xlsx.File// file is the xlsx file.
sheets[]*Sheet// sheets is the list of sheets in the file.
sheetsMapmap[string]*Sheet// sheetsMap is a map of sheets in the file.
}// Sheet represents a sheet in a xlsx file.
typeSheetstruct{namestring// name of the sheet.
rows[]*Row// rows in the sheet.
}// Row represents a row in a sheet.
typeRowstruct{cells[]*Cell// cells in the row.
heightfloat64// height of the row.
}// Cell represents a cell in a row.
typeCellstruct{Contentany// content of the cell.
MergeH,MergeVint// number of cells to merge horizontally and vertically, respectively.
Style*CellStyle// style of the cell.
}// CellStyle is the style of the cell.
typeCellStylexlsx.Style
Now, here is an updated version of our main function from before. This time, we implement custom methods on the Sheet and Row structs.
Note: this implementation is different from the one in the table package.
And here is a picture of the table we just created:
See ? Instead of having to add each cell - and its contents - manually, we can just batch them together using the AddCells method.
This is just one feature of the table package, other features include:
// generate a generic table that has a title, headings, and body.
funcGenericTable(titlestring,headings[]*Cell,rows[]*Row,borderStylestring)[]*Row// generate a title row.
funcRowTitle(titlestring,colsint,borderStylestring,cellStyle...*CellStyle)*Row// generate rows with cells that are merged horizontally and vertically.
funcRowWithMergedCells(cells...*Cell)[]*Row