xml marshal without indent and use buffer in string concatenation.

This commit is contained in:
Ri Xu 2016-09-04 19:25:31 +08:00
parent 78e79347b6
commit 9c3a24d5c3
2 changed files with 41 additions and 18 deletions

View File

@ -2,6 +2,7 @@ package excelize
import (
"archive/zip"
"bytes"
"encoding/xml"
"fmt"
"strconv"
@ -48,7 +49,7 @@ func SetCellInt(file []FileList, sheet string, axis string, value int) []FileLis
xlsx.SheetData.Row[xAxis].C[yAxis].T = ""
xlsx.SheetData.Row[xAxis].C[yAxis].V = strconv.Itoa(value)
output, err := xml.MarshalIndent(xlsx, "", "")
output, err := xml.Marshal(xlsx)
if err != nil {
fmt.Println(err)
}
@ -78,7 +79,7 @@ func SetCellStr(file []FileList, sheet string, axis string, value string) []File
xlsx.SheetData.Row[xAxis].C[yAxis].T = "str"
xlsx.SheetData.Row[xAxis].C[yAxis].V = value
output, err := xml.MarshalIndent(xlsx, "", "")
output, err := xml.Marshal(xlsx)
if err != nil {
fmt.Println(err)
}
@ -95,13 +96,17 @@ func completeCol(xlsx xlsxWorksheet, row int, cell int) xlsxWorksheet {
})
}
}
buffer := bytes.Buffer{}
for k, v := range xlsx.SheetData.Row {
if len(v.C) < cell {
start := len(v.C)
for iii := start; iii < cell; iii++ {
buffer.WriteString(toAlphaString(iii + 1))
buffer.WriteString(strconv.Itoa(k + 1))
xlsx.SheetData.Row[k].C = append(xlsx.SheetData.Row[k].C, xlsxC{
R: toAlphaString(iii+1) + strconv.Itoa(k+1),
R: buffer.String(),
})
buffer.Reset()
}
}
}
@ -116,14 +121,17 @@ func completeRow(xlsx xlsxWorksheet, row int, cell int) xlsxWorksheet {
R: i + 1,
})
}
buffer := bytes.Buffer{}
for ii := 0; ii < row; ii++ {
start := len(xlsx.SheetData.Row[ii].C)
if start == 0 {
for iii := start; iii < cell; iii++ {
buffer.WriteString(toAlphaString(iii + 1))
buffer.WriteString(strconv.Itoa(ii + 1))
xlsx.SheetData.Row[ii].C = append(xlsx.SheetData.Row[ii].C, xlsxC{
R: toAlphaString(iii+1) + strconv.Itoa(ii+1),
R: buffer.String(),
})
buffer.Reset()
}
}
}
@ -170,6 +178,7 @@ func replaceWorkSheetsRelationshipsNameSpace(workbookMarshal string) string {
// </row>
//
func checkRow(xlsx xlsxWorksheet) xlsxWorksheet {
buffer := bytes.Buffer{}
for k, v := range xlsx.SheetData.Row {
lenCol := len(v.C)
if lenCol < 1 {
@ -183,10 +192,12 @@ func checkRow(xlsx xlsxWorksheet) xlsxWorksheet {
xlsx.SheetData.Row[k].C = xlsx.SheetData.Row[k].C[:0]
tmp := []xlsxC{}
for i := 0; i <= endCol; i++ {
fixAxis := toAlphaString(i+1) + strconv.Itoa(endRow)
buffer.WriteString(toAlphaString(i + 1))
buffer.WriteString(strconv.Itoa(endRow))
tmp = append(tmp, xlsxC{
R: fixAxis,
R: buffer.String(),
})
buffer.Reset()
}
xlsx.SheetData.Row[k].C = tmp
for _, y := range oldRow {

View File

@ -1,6 +1,7 @@
package excelize
import (
"bytes"
"encoding/xml"
"fmt"
"strconv"
@ -32,7 +33,7 @@ func setContentTypes(file []FileList, index int) []FileList {
PartName: `/xl/worksheets/sheet` + strconv.Itoa(index) + `.xml`,
ContentType: "application/vnd.openxmlformats-officedocument.spreadsheetml.worksheet+xml",
})
output, err := xml.MarshalIndent(content, "", "")
output, err := xml.Marshal(content)
if err != nil {
fmt.Println(err)
}
@ -46,7 +47,7 @@ func setSheet(file []FileList, index int) []FileList {
xlsx.SheetViews.SheetView = append(xlsx.SheetViews.SheetView, xlsxSheetView{
WorkbookViewID: 0,
})
output, err := xml.MarshalIndent(xlsx, "", "")
output, err := xml.Marshal(xlsx)
if err != nil {
fmt.Println(err)
}
@ -66,7 +67,7 @@ func setWorkbook(file []FileList, index int, name string) []FileList {
SheetID: strconv.Itoa(index),
ID: "rId" + strconv.Itoa(rID),
})
output, err := xml.MarshalIndent(content, "", "")
output, err := xml.Marshal(content)
if err != nil {
fmt.Println(err)
}
@ -84,12 +85,19 @@ func readXlsxWorkbookRels(file []FileList) xlsxWorkbookRels {
func addXlsxWorkbookRels(file []FileList, sheet int) []FileList {
content := readXlsxWorkbookRels(file)
rID := len(content.Relationships) + 1
ID := bytes.Buffer{}
ID.WriteString("rId")
ID.WriteString(strconv.Itoa(rID))
target := bytes.Buffer{}
target.WriteString(`worksheets/sheet`)
target.WriteString(strconv.Itoa(sheet))
target.WriteString(`.xml`)
content.Relationships = append(content.Relationships, xlsxWorkbookRelation{
ID: "rId" + strconv.Itoa(rID),
Target: `worksheets/sheet` + strconv.Itoa(sheet) + `.xml`,
ID: ID.String(),
Target: target.String(),
Type: "http://schemas.openxmlformats.org/officeDocument/2006/relationships/worksheet",
})
output, err := xml.MarshalIndent(content, "", "")
output, err := xml.Marshal(content)
if err != nil {
fmt.Println(err)
}
@ -141,17 +149,20 @@ func SetActiveSheet(file []FileList, index int) []FileList {
})
}
sheets := len(content.Sheets.Sheet)
output, err := xml.MarshalIndent(content, "", "")
output, err := xml.Marshal(content)
if err != nil {
fmt.Println(err)
}
file = saveFileList(file, `xl/workbook.xml`, workBookCompatibility(replaceRelationshipsNameSpace(string(output))))
index++
buffer := bytes.Buffer{}
for i := 0; i < sheets; i++ {
xlsx := xlsxWorksheet{}
sheetIndex := i + 1
path := `xl/worksheets/sheet` + strconv.Itoa(sheetIndex) + `.xml`
xml.Unmarshal([]byte(readXML(file, path)), &xlsx)
buffer.WriteString(`xl/worksheets/sheet`)
buffer.WriteString(strconv.Itoa(sheetIndex))
buffer.WriteString(`.xml`)
xml.Unmarshal([]byte(readXML(file, buffer.String())), &xlsx)
if index == sheetIndex {
if len(xlsx.SheetViews.SheetView) > 0 {
xlsx.SheetViews.SheetView[0].TabSelected = true
@ -165,11 +176,12 @@ func SetActiveSheet(file []FileList, index int) []FileList {
xlsx.SheetViews.SheetView[0].TabSelected = false
}
}
sheet, err := xml.MarshalIndent(xlsx, "", "")
sheet, err := xml.Marshal(xlsx)
if err != nil {
fmt.Println(err)
}
file = saveFileList(file, path, replaceRelationshipsID(replaceWorkSheetsRelationshipsNameSpace(string(sheet))))
file = saveFileList(file, buffer.String(), replaceRelationshipsID(replaceWorkSheetsRelationshipsNameSpace(string(sheet))))
buffer.Reset()
}
return file
}