Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue #539 Fixed error opening excel file created in encoding d… #540

Merged
merged 6 commits into from
Dec 19, 2019
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
~$*.xlsx
test/Test*.xlsx
*.out
*.test
*.test
.idea
monoflash marked this conversation as resolved.
Show resolved Hide resolved
23 changes: 19 additions & 4 deletions calcchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,31 @@

package excelize

import "encoding/xml"
import (
"bytes"
"encoding/xml"
"io"
"log"
monoflash marked this conversation as resolved.
Show resolved Hide resolved
)

// calcChainReader provides a function to get the pointer to the structure
// after deserialization of xl/calcChain.xml.
func (f *File) calcChainReader() *xlsxCalcChain {
var (
err error
decoder *xml.Decoder
)

if f.CalcChain == nil {
var c xlsxCalcChain
_ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML("xl/calcChain.xml")), &c)
f.CalcChain = &c
f.CalcChain = new(xlsxCalcChain)

decoder = xml.NewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML("xl/calcChain.xml"))))
decoder.CharsetReader = CharsetReader
if err = decoder.Decode(f.CalcChain); err != nil && err != io.EOF {
log.Printf("xml decode error: %s", err)
}
}

return f.CalcChain
}

Expand Down
38 changes: 38 additions & 0 deletions charset_reader.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package excelize

import (
"fmt"
"io"
"regexp"
"strings"

"golang.org/x/text/encoding"
"golang.org/x/text/encoding/charmap"
)

// CharsetReader Decoder from all codepages to UTF-8
func CharsetReader(charset string, input io.Reader) (rdr io.Reader, err error) {
monoflash marked this conversation as resolved.
Show resolved Hide resolved
monoflash marked this conversation as resolved.
Show resolved Hide resolved
var (
sm, nm string
item, enc encoding.Encoding
i int
rexReplaceCharsetName = regexp.MustCompile(`[-_ ]`)
)

sm = rexReplaceCharsetName.ReplaceAllString(strings.ToLower(charset), ``)
for i = range charmap.All {
item = charmap.All[i]
nm = rexReplaceCharsetName.ReplaceAllString(strings.ToLower(fmt.Sprintf("%s", item)), ``)
if strings.EqualFold(sm, nm) {
enc = item
break
}
}
if enc == nil {
err = fmt.Errorf("CharsetReader: unexpected charset: %q", charset)
return
}
rdr = enc.NewDecoder().Reader(input)

return
}
112 changes: 112 additions & 0 deletions charset_reader_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package excelize

//import "gopkg.in/webnice/log.v2"
monoflash marked this conversation as resolved.
Show resolved Hide resolved
import (
"bytes"
"io"
"io/ioutil"
"testing"

"golang.org/x/text/encoding/charmap"
)

func TestCharsetReaderSupported(t *testing.T) {
const unknownCodepage = `DEE26011`
var (
err error
inp io.Reader
out io.Reader
i int
supportedCodepages = []string{
charmap.CodePage037.String(),
charmap.CodePage437.String(),
charmap.CodePage037.String(),
charmap.CodePage850.String(),
charmap.CodePage852.String(),
charmap.CodePage855.String(),
charmap.CodePage858.String(),
charmap.CodePage860.String(),
charmap.CodePage862.String(),
charmap.CodePage863.String(),
charmap.CodePage865.String(),
charmap.CodePage866.String(),
charmap.CodePage1047.String(),
charmap.CodePage1140.String(),
charmap.ISO8859_1.String(),
charmap.ISO8859_2.String(),
charmap.ISO8859_3.String(),
charmap.ISO8859_4.String(),
charmap.ISO8859_5.String(),
charmap.ISO8859_6.String(),
charmap.ISO8859_7.String(),
charmap.ISO8859_8.String(),
charmap.ISO8859_9.String(),
charmap.ISO8859_10.String(),
charmap.ISO8859_13.String(),
charmap.ISO8859_14.String(),
charmap.ISO8859_15.String(),
charmap.ISO8859_16.String(),
charmap.KOI8R.String(),
charmap.KOI8U.String(),
charmap.Macintosh.String(),
charmap.MacintoshCyrillic.String(),
charmap.Windows874.String(),
charmap.Windows1250.String(),
charmap.Windows1251.String(),
charmap.Windows1252.String(),
charmap.Windows1253.String(),
charmap.Windows1254.String(),
charmap.Windows1255.String(),
charmap.Windows1256.String(),
charmap.Windows1257.String(),
charmap.Windows1258.String(),
}
)

inp = bytes.NewReader([]byte{})
// Unknown codepage
if out, err = CharsetReader(unknownCodepage, inp); err == nil || out != nil {
t.Fatalf("incorrect CharsetReader implementation")
}
// Known codepages
for i = range supportedCodepages {
if out, err = CharsetReader(supportedCodepages[i], inp); err != nil || out == nil {
t.Fatalf("codepage %q is not supported", supportedCodepages[i])
}
}
}

func TestCharsetReaderTranslation(t *testing.T) {
var (
err error
out io.Reader
buf []byte
destination = []byte{
0xD0, 0x9F, 0xD1, 0x80, 0xD0, 0xB8, 0xD0, 0xB2, 0xD0, 0xB5,
0xD1, 0x82, 0x20, 0xD0, 0xBC, 0xD0, 0xB8, 0xD1, 0x80,
}
tests = []struct {
Source []byte
Destination []byte
Name string
}{
{[]byte{0xF0, 0xD2, 0xC9, 0xD7, 0xC5, 0xD4, 0x20, 0xCD, 0xC9, 0xD2}, destination, "koi8-r"},
{[]byte{0x8F, 0xE0, 0xA8, 0xA2, 0xA5, 0xE2, 0x20, 0xAC, 0xA8, 0xE0}, destination, "ibm code page 866"},
{[]byte{0xCF, 0xF0, 0xE8, 0xE2, 0xE5, 0xF2, 0x20, 0xEC, 0xE8, 0xF0}, destination, "windows-1251"},
{[]byte{0x8F, 0xF0, 0xE8, 0xE2, 0xE5, 0xF2, 0x20, 0xEC, 0xE8, 0xF0}, destination, "macintosh cyrillic"},
}
)

for _, test := range tests {
if out, err = CharsetReader(test.Name, bytes.NewReader(test.Source)); err != nil {
t.Fatalf("incorrect CharsetReader implementation, error: %s", err)
}
if buf, err = ioutil.ReadAll(out); err != nil {
t.Fatalf("ioutil ReadAll error: %s", err)
}
if !bytes.EqualFold(buf, test.Destination) {
t.Fatalf("transcode error, incorrect decoding %s", test.Name)
}
buf, out = buf[:0], nil
}
}
17 changes: 14 additions & 3 deletions chart.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
package excelize

import (
"bytes"
"encoding/json"
"encoding/xml"
"errors"
"log"
"strconv"
"strings"
)
Expand Down Expand Up @@ -1735,14 +1737,23 @@ func (f *File) drawPlotAreaTxPr() *cTxPr {
// deserialization, two different structures: decodeWsDr and encodeWsDr are
// defined.
func (f *File) drawingParser(path string) (*xlsxWsDr, int) {
var (
err error
decoder *xml.Decoder
ok bool
)

if f.Drawings[path] == nil {
content := xlsxWsDr{}
content.A = NameSpaceDrawingML
content.Xdr = NameSpaceDrawingMLSpreadSheet
_, ok := f.XLSX[path]
if ok { // Append Model
if _, ok = f.XLSX[path]; ok { // Append Model
decodeWsDr := decodeWsDr{}
_ = xml.Unmarshal(namespaceStrictToTransitional(f.readXML(path)), &decodeWsDr)
decoder = xml.NewDecoder(bytes.NewReader(namespaceStrictToTransitional(f.readXML(path))))
decoder.CharsetReader = CharsetReader
if err = decoder.Decode(&decodeWsDr); err != nil {
log.Printf("xml decode error: %s", err)
}
content.R = decodeWsDr.R
for _, v := range decodeWsDr.OneCellAnchor {
content.OneCellAnchor = append(content.OneCellAnchor, &xdrCellAnchor{
Expand Down
30 changes: 24 additions & 6 deletions comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
package excelize

import (
"bytes"
"encoding/json"
"encoding/xml"
"fmt"
"log"
"strconv"
"strings"
)
Expand Down Expand Up @@ -303,12 +305,20 @@ func (f *File) countComments() int {
// decodeVMLDrawingReader provides a function to get the pointer to the
// structure after deserialization of xl/drawings/vmlDrawing%d.xml.
func (f *File) decodeVMLDrawingReader(path string) *decodeVmlDrawing {
var (
err error
decoder *xml.Decoder
)

if f.DecodeVMLDrawing[path] == nil {
c, ok := f.XLSX[path]
if ok {
d := decodeVmlDrawing{}
_ = xml.Unmarshal(namespaceStrictToTransitional(c), &d)
f.DecodeVMLDrawing[path] = &d
f.DecodeVMLDrawing[path] = new(decodeVmlDrawing)
decoder = xml.NewDecoder(bytes.NewReader(namespaceStrictToTransitional(c)))
decoder.CharsetReader = CharsetReader
if err = decoder.Decode(f.DecodeVMLDrawing[path]); err != nil {
log.Printf("xml decode error: %s", err)
}
}
}
return f.DecodeVMLDrawing[path]
Expand All @@ -328,12 +338,20 @@ func (f *File) vmlDrawingWriter() {
// commentsReader provides a function to get the pointer to the structure
// after deserialization of xl/comments%d.xml.
func (f *File) commentsReader(path string) *xlsxComments {
var (
err error
decoder *xml.Decoder
)

if f.Comments[path] == nil {
content, ok := f.XLSX[path]
if ok {
c := xlsxComments{}
_ = xml.Unmarshal(namespaceStrictToTransitional(content), &c)
f.Comments[path] = &c
f.Comments[path] = new(xlsxComments)
decoder = xml.NewDecoder(bytes.NewReader(namespaceStrictToTransitional(content)))
decoder.CharsetReader = CharsetReader
if err = decoder.Decode(f.Comments[path]); err != nil {
log.Printf("xml decode error: %s", err)
}
}
}
return f.Comments[path]
Expand Down
Loading