@ -21,6 +21,7 @@ import (
"io/ioutil"
"io/ioutil"
"os"
"os"
"path"
"path"
"path/filepath"
"strconv"
"strconv"
"strings"
"strings"
"sync"
"sync"
@ -59,21 +60,27 @@ type charsetTranscoderFn func(charset string, input io.Reader) (rdr io.Reader, e
// Options define the options for open spreadsheet.
// Options define the options for open spreadsheet.
type Options struct {
type Options struct {
Password string
Password string
UnzipSizeLimit int64
}
}
// OpenFile take the name of an spreadsheet file and returns a populated spreadsheet file struct
// OpenFile take the name of an spreadsheet file and returns a populated
// for it. For example, open spreadsheet with password protection:
// spreadsheet file struct for it. For example, open spreadsheet with
// password protection:
//
//
// f, err := excelize.OpenFile("Book1.xlsx", excelize.Options{Password: "password"})
// f, err := excelize.OpenFile("Book1.xlsx", excelize.Options{Password: "password"})
// if err != nil {
// if err != nil {
// return
// return
// }
// }
//
//
// Note that the excelize just support decrypt and not support encrypt currently, the spreadsheet
// Note that the excelize just support decrypt and not support encrypt
// saved by Save and SaveAs will be without password unprotected.
// currently, the spreadsheet saved by Save and SaveAs will be without
// password unprotected.
//
// UnzipSizeLimit specified the unzip size limit in bytes on open the
// spreadsheet, the default size limit is 16GB.
func OpenFile ( filename string , opt ... Options ) ( * File , error ) {
func OpenFile ( filename string , opt ... Options ) ( * File , error ) {
file , err := os . Open ( filename )
file , err := os . Open ( file path. Clean ( file name) )
if err != nil {
if err != nil {
return nil , err
return nil , err
}
}
@ -89,6 +96,7 @@ func OpenFile(filename string, opt ...Options) (*File, error) {
// newFile is object builder
// newFile is object builder
func newFile ( ) * File {
func newFile ( ) * File {
return & File {
return & File {
options : & Options { UnzipSizeLimit : UnzipSizeLimit } ,
xmlAttr : make ( map [ string ] [ ] xml . Attr ) ,
xmlAttr : make ( map [ string ] [ ] xml . Attr ) ,
checked : make ( map [ string ] bool ) ,
checked : make ( map [ string ] bool ) ,
sheetMap : make ( map [ string ] string ) ,
sheetMap : make ( map [ string ] string ) ,
@ -111,10 +119,13 @@ func OpenReader(r io.Reader, opt ...Options) (*File, error) {
return nil , err
return nil , err
}
}
f := newFile ( )
f := newFile ( )
if bytes . Contains ( b , oleIdentifier ) && len ( opt ) > 0 {
for i := range opt {
for _ , o := range opt {
f . options = & opt [ i ]
f . options = & o
if f . options . UnzipSizeLimit == 0 {
f . options . UnzipSizeLimit = UnzipSizeLimit
}
}
}
if bytes . Contains ( b , oleIdentifier ) {
b , err = Decrypt ( b , f . options )
b , err = Decrypt ( b , f . options )
if err != nil {
if err != nil {
return nil , fmt . Errorf ( "decrypted file failed" )
return nil , fmt . Errorf ( "decrypted file failed" )
@ -124,8 +135,7 @@ func OpenReader(r io.Reader, opt ...Options) (*File, error) {
if err != nil {
if err != nil {
return nil , err
return nil , err
}
}
file , sheetCount , err := ReadZipReader ( zr , f . options )
file , sheetCount , err := ReadZipReader ( zr )
if err != nil {
if err != nil {
return nil , err
return nil , err
}
}
@ -316,18 +326,18 @@ func (f *File) UpdateLinkedValue() error {
// recalculate formulas
// recalculate formulas
wb . CalcPr = nil
wb . CalcPr = nil
for _ , name := range f . GetSheetList ( ) {
for _ , name := range f . GetSheetList ( ) {
xlsx , err := f . workSheetReader ( name )
ws , err := f . workSheetReader ( name )
if err != nil {
if err != nil {
if err . Error ( ) == fmt . Sprintf ( "sheet %s is chart sheet" , trimSheetName ( name ) ) {
if err . Error ( ) == fmt . Sprintf ( "sheet %s is chart sheet" , trimSheetName ( name ) ) {
continue
continue
}
}
return err
return err
}
}
for indexR := range xlsx . SheetData . Row {
for indexR := range ws . SheetData . Row {
for indexC , col := range xlsx . SheetData . Row [ indexR ] . C {
for indexC , col := range ws . SheetData . Row [ indexR ] . C {
if col . F != nil && col . V != "" {
if col . F != nil && col . V != "" {
xlsx . SheetData . Row [ indexR ] . C [ indexC ] . V = ""
ws . SheetData . Row [ indexR ] . C [ indexC ] . V = ""
xlsx . SheetData . Row [ indexR ] . C [ indexC ] . T = ""
ws . SheetData . Row [ indexR ] . C [ indexC ] . T = ""
}
}
}
}
}
}
@ -381,7 +391,7 @@ func (f *File) AddVBAProject(bin string) error {
Type : SourceRelationshipVBAProject ,
Type : SourceRelationshipVBAProject ,
} )
} )
}
}
file , _ := ioutil . ReadFile ( bin)
file , _ := ioutil . ReadFile ( filepath. Clean ( bin) )
f . Pkg . Store ( "xl/vbaProject.bin" , file )
f . Pkg . Store ( "xl/vbaProject.bin" , file )
return err
return err
}
}