forked from vdjagilev/nmap-formatter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathformatter_excel.go
70 lines (55 loc) · 2.16 KB
/
formatter_excel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package formatter
import (
"fmt"
"github.com/xuri/excelize/v2"
)
// ExcelFormatter is struct defined for Excel Output use-case
type ExcelFormatter struct {
config *Config
}
func (f *ExcelFormatter) Format(td *TemplateData, templateContent string) (err error) {
file := excelize.NewFile()
sheetName := "Sheet1"
// Create a style for center alignment
style, err := file.NewStyle(&excelize.Style{
Alignment: &excelize.Alignment{Horizontal: "center", Vertical: "center"},
})
// Set the column headers
file.SetCellValue(sheetName, "A1", "IP/Host")
file.SetCellValue(sheetName, "B1", "Servicios")
file.SetCellStyle(sheetName, "A1", "A1", style)
file.SetCellStyle(sheetName, "B1", "B1", style)
row := 2 // Start from row 2 for data
for i := range td.NMAPRun.Host {
var host *Host = &td.NMAPRun.Host[i]
// Skipping hosts that are down
if td.OutputOptions.ExcelOptions.SkipDownHosts && host.Status.State != "up" {
continue
}
address := fmt.Sprintf("%s (%s)", host.JoinedAddresses("/"), host.JoinedHostNames("/"))
// Set the IP/Host value
cell := fmt.Sprintf("A%d", row)
file.SetCellValue(sheetName, cell, address)
file.SetCellStyle(sheetName, cell, cell, style)
startRow := row // Remember the start row for this host
for j := range host.Port {
var port *Port = &host.Port[j]
col := 'B' // Start from column B for Services
// Set the Service value
cell = fmt.Sprintf("%c%d", col, row)
file.SetCellValue(sheetName, cell, fmt.Sprintf("%d/%s %s", port.PortID, port.Protocol, port.Service.Name))
file.SetCellStyle(sheetName, cell, cell, style)
row++ // Increment row for next service
}
// Merge cells in the IP/Host column for this host
if row > startRow+1 { // Only merge if there's more than one service
file.MergeCell(sheetName, fmt.Sprintf("A%d", startRow), fmt.Sprintf("A%d", row-1))
}
}
// Save the Excel file
err = file.SaveAs("nmap-output.xlsx")
return err
}
func (f *ExcelFormatter) defaultTemplateContent() string {
return HTMLSimpleTemplate
}