I need to create an Excel report and download it. The data is taken from SQL (approximately 200 thousand rows with 70 columns). The number of columns is not known in advance (otherwise I would just have stuffed everything into the structure and norms), since pivot is used for normal output, without it there would be about 20 columns for 1,400 thousand lines. The request itself takes about two minutes - bad, but it will do. Then there is an entry in each Excel cell separately, because I did not find any other way to transfer all this, here it is (using "github.com/360EntSecGroup-Skylar/excelize" and _ "github.com/denisenkom/go-mssqldb" // s ):
rows, err := db.Query("[Proc]", sql.Named("param", param)) if err != nil { log.Fatal(err) } defer rows.Close() ( colNames, err := rows.Columns() if err != nil { fmt.Println("error fetching column names\n", err) } length := len(colNames) // Create a interface slice filled with pointers to interface{}'s pointers := make([]interface{}, length) container := make([]interface{}, length) for i := range pointers { pointers[i] = &container[i] } rowcol := 9 col := 1 for i := 0; i < len(colNames); i++ { xxxfile.SetCellValue(sheetName, getExcelColumnName(col)+"8", colNames[i]) col++ } // Process sql rows for rows.Next() { column := 1 // Scan the sql rows into the interface{} slice err = rows.Scan(pointers...) if err != nil { fmt.Println("error scanning sql row\n", err) } // Here we range over our container and look at each column // and set some different options depending on the column type. for _, v := range container { xxxfile.SetCellValue(sheetName, getExcelColumnName(column)+strconv.Itoa(rowcol), v) column++ } rowcol++ }
After the recording, each line is processed in a cycle (there is a group attribute in the line, according to which it is formatted and the style is assigned)
How to optimize it? Must be reduced to a few minutes
github.com/denisenkom/go-mssqldb
- which version of MSSQL? b) Do you have direct access to the tables or only through the stored procedure[Proc]
? c) did you try to prepare a csv file instead of an excel file and then use it as a source? - Alex Yu