|
| 1 | +package excelize |
| 2 | + |
| 3 | +import ( |
| 4 | + "archive/zip" |
| 5 | + "encoding/xml" |
| 6 | + "fmt" |
| 7 | + "strconv" |
| 8 | + "strings" |
| 9 | +) |
| 10 | + |
| 11 | +type FileList struct { |
| 12 | + Key string |
| 13 | + Value string |
| 14 | +} |
| 15 | + |
| 16 | +// OpenFile() take the name of an XLSX file and returns a populated |
| 17 | +// xlsx.File struct for it. |
| 18 | +func OpenFile(filename string) (file []FileList, err error) { |
| 19 | + var f *zip.ReadCloser |
| 20 | + f, err = zip.OpenReader(filename) |
| 21 | + if err != nil { |
| 22 | + return nil, err |
| 23 | + } |
| 24 | + file, err = ReadZip(f) |
| 25 | + return |
| 26 | +} |
| 27 | + |
| 28 | +// Set int type value of a cell |
| 29 | +func SetCellInt(file []FileList, sheet string, axis string, value int) []FileList { |
| 30 | + axis = strings.ToUpper(axis) |
| 31 | + var xlsx xlsxWorksheet |
| 32 | + col := getColIndex(axis) |
| 33 | + row := getRowIndex(axis) |
| 34 | + xAxis := row - 1 |
| 35 | + yAxis := titleToNumber(col) |
| 36 | + |
| 37 | + name := fmt.Sprintf("xl/worksheets/%s.xml", strings.ToLower(sheet)) |
| 38 | + xml.Unmarshal([]byte(readXml(file, name)), &xlsx) |
| 39 | + |
| 40 | + rows := xAxis + 1 |
| 41 | + cell := yAxis + 1 |
| 42 | + |
| 43 | + xlsx = checkRow(xlsx) |
| 44 | + |
| 45 | + xlsx = completeRow(xlsx, rows, cell) |
| 46 | + xlsx = completeCol(xlsx, rows, cell) |
| 47 | + |
| 48 | + xlsx.SheetData.Row[xAxis].C[yAxis].T = "" |
| 49 | + xlsx.SheetData.Row[xAxis].C[yAxis].V = strconv.Itoa(value) |
| 50 | + |
| 51 | + output, err := xml.MarshalIndent(xlsx, "", "") |
| 52 | + if err != nil { |
| 53 | + fmt.Println(err) |
| 54 | + } |
| 55 | + saveFileList(file, name, replaceRelationshipsID(replaceWorkSheetsRelationshipsNameSpace(string(output)))) |
| 56 | + return file |
| 57 | +} |
| 58 | + |
| 59 | +// Set string type value of a cell |
| 60 | +func SetCellStr(file []FileList, sheet string, axis string, value string) []FileList { |
| 61 | + axis = strings.ToUpper(axis) |
| 62 | + var xlsx xlsxWorksheet |
| 63 | + col := getColIndex(axis) |
| 64 | + row := getRowIndex(axis) |
| 65 | + xAxis := row - 1 |
| 66 | + yAxis := titleToNumber(col) |
| 67 | + |
| 68 | + name := fmt.Sprintf("xl/worksheets/%s.xml", strings.ToLower(sheet)) |
| 69 | + xml.Unmarshal([]byte(readXml(file, name)), &xlsx) |
| 70 | + |
| 71 | + rows := xAxis + 1 |
| 72 | + cell := yAxis + 1 |
| 73 | + |
| 74 | + xlsx = checkRow(xlsx) |
| 75 | + xlsx = completeRow(xlsx, rows, cell) |
| 76 | + xlsx = completeCol(xlsx, rows, cell) |
| 77 | + |
| 78 | + xlsx.SheetData.Row[xAxis].C[yAxis].T = "str" |
| 79 | + xlsx.SheetData.Row[xAxis].C[yAxis].V = value |
| 80 | + |
| 81 | + output, err := xml.MarshalIndent(xlsx, "", "") |
| 82 | + if err != nil { |
| 83 | + fmt.Println(err) |
| 84 | + } |
| 85 | + saveFileList(file, name, replaceRelationshipsID(replaceWorkSheetsRelationshipsNameSpace(string(output)))) |
| 86 | + return file |
| 87 | +} |
| 88 | + |
| 89 | +// Completion column element tags of XML in a sheet |
| 90 | +func completeCol(xlsx xlsxWorksheet, row int, cell int) xlsxWorksheet { |
| 91 | + if len(xlsx.SheetData.Row) < cell { |
| 92 | + for i := len(xlsx.SheetData.Row); i < cell; i++ { |
| 93 | + xlsx.SheetData.Row = append(xlsx.SheetData.Row, xlsxRow{ |
| 94 | + R: i + 1, |
| 95 | + }) |
| 96 | + } |
| 97 | + } |
| 98 | + for k, v := range xlsx.SheetData.Row { |
| 99 | + if len(v.C) < cell { |
| 100 | + start := len(v.C) |
| 101 | + for iii := start; iii < cell; iii++ { |
| 102 | + xlsx.SheetData.Row[k].C = append(xlsx.SheetData.Row[k].C, xlsxC{ |
| 103 | + R: toAlphaString(iii+1) + strconv.Itoa(k+1), |
| 104 | + }) |
| 105 | + } |
| 106 | + } |
| 107 | + } |
| 108 | + return xlsx |
| 109 | +} |
| 110 | + |
| 111 | +// Completion row element tags of XML in a sheet |
| 112 | +func completeRow(xlsx xlsxWorksheet, row int, cell int) xlsxWorksheet { |
| 113 | + if len(xlsx.SheetData.Row) < row { |
| 114 | + for i := len(xlsx.SheetData.Row); i < row; i++ { |
| 115 | + xlsx.SheetData.Row = append(xlsx.SheetData.Row, xlsxRow{ |
| 116 | + R: i + 1, |
| 117 | + }) |
| 118 | + } |
| 119 | + |
| 120 | + for ii := 0; ii < row; ii++ { |
| 121 | + start := len(xlsx.SheetData.Row[ii].C) |
| 122 | + if start == 0 { |
| 123 | + for iii := start; iii < cell; iii++ { |
| 124 | + xlsx.SheetData.Row[ii].C = append(xlsx.SheetData.Row[ii].C, xlsxC{ |
| 125 | + R: toAlphaString(iii+1) + strconv.Itoa(ii+1), |
| 126 | + }) |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | + } |
| 131 | + return xlsx |
| 132 | +} |
| 133 | + |
| 134 | +// Replace xl/worksheets/sheet%d.xml XML tags to self-closing for compatible Office Excel 2007 |
| 135 | +func replaceWorkSheetsRelationshipsNameSpace(workbookMarshal string) string { |
| 136 | + oldXmlns := `<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">` |
| 137 | + newXmlns := `<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mx="http://schemas.microsoft.com/office/mac/excel/2008/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:xm="http://schemas.microsoft.com/office/excel/2006/main">` |
| 138 | + workbookMarshal = strings.Replace(workbookMarshal, oldXmlns, newXmlns, -1) |
| 139 | + workbookMarshal = strings.Replace(workbookMarshal, `></sheetPr>`, ` />`, -1) |
| 140 | + workbookMarshal = strings.Replace(workbookMarshal, `></dimension>`, ` />`, -1) |
| 141 | + workbookMarshal = strings.Replace(workbookMarshal, `></selection>`, ` />`, -1) |
| 142 | + workbookMarshal = strings.Replace(workbookMarshal, `></sheetFormatPr>`, ` />`, -1) |
| 143 | + workbookMarshal = strings.Replace(workbookMarshal, `></printOptions>`, ` />`, -1) |
| 144 | + workbookMarshal = strings.Replace(workbookMarshal, `></pageSetup>`, ` />`, -1) |
| 145 | + workbookMarshal = strings.Replace(workbookMarshal, `></pageMargins>`, ` />`, -1) |
| 146 | + workbookMarshal = strings.Replace(workbookMarshal, `></headerFooter>`, ` />`, -1) |
| 147 | + workbookMarshal = strings.Replace(workbookMarshal, `></drawing>`, ` />`, -1) |
| 148 | + return workbookMarshal |
| 149 | +} |
| 150 | + |
| 151 | +// Check XML tags and fix discontinuous case, for example: |
| 152 | +// |
| 153 | +// <row r="15" spans="1:22" x14ac:dyDescent="0.2"> |
| 154 | +// <c r="A15" s="2" /> |
| 155 | +// <c r="B15" s="2" /> |
| 156 | +// <c r="F15" s="1" /> |
| 157 | +// <c r="G15" s="1" /> |
| 158 | +// </row> |
| 159 | +// |
| 160 | +// in this case, we should to change it to |
| 161 | +// |
| 162 | +// <row r="15" spans="1:22" x14ac:dyDescent="0.2"> |
| 163 | +// <c r="A15" s="2" /> |
| 164 | +// <c r="B15" s="2" /> |
| 165 | +// <c r="C15" s="2" /> |
| 166 | +// <c r="D15" s="2" /> |
| 167 | +// <c r="E15" s="2" /> |
| 168 | +// <c r="F15" s="1" /> |
| 169 | +// <c r="G15" s="1" /> |
| 170 | +// </row> |
| 171 | +// |
| 172 | +func checkRow(xlsx xlsxWorksheet) xlsxWorksheet { |
| 173 | + for k, v := range xlsx.SheetData.Row { |
| 174 | + lenCol := len(v.C) |
| 175 | + endR := getColIndex(v.C[lenCol-1].R) |
| 176 | + endRow := getRowIndex(v.C[lenCol-1].R) |
| 177 | + endCol := titleToNumber(endR) |
| 178 | + if lenCol < endCol { |
| 179 | + oldRow := xlsx.SheetData.Row[k].C |
| 180 | + xlsx.SheetData.Row[k].C = xlsx.SheetData.Row[k].C[:0] |
| 181 | + tmp := []xlsxC{} |
| 182 | + for i := 0; i <= endCol; i++ { |
| 183 | + fixAxis := toAlphaString(i+1) + strconv.Itoa(endRow) |
| 184 | + tmp = append(tmp, xlsxC{ |
| 185 | + R: fixAxis, |
| 186 | + }) |
| 187 | + } |
| 188 | + xlsx.SheetData.Row[k].C = tmp |
| 189 | + for _, y := range oldRow { |
| 190 | + colAxis := titleToNumber(getColIndex(y.R)) |
| 191 | + xlsx.SheetData.Row[k].C[colAxis] = y |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | + return xlsx |
| 196 | +} |
0 commit comments