Browse Source

Merge branch 'pr-mac-number' of https://github.com/bronze1man/xlsx

Geoffrey J. Teale 12 years ago
parent
commit
591bf2a4b1
3 changed files with 39 additions and 1 deletions
  1. BIN
      macExcelTest.xlsx
  2. 22 0
      macExcel_test.go
  3. 17 1
      workbook.go

BIN
macExcelTest.xlsx


+ 22 - 0
macExcel_test.go

@@ -0,0 +1,22 @@
+package xlsx
+
+import (
+	"testing"
+)
+
+func TestMacExcel(t *testing.T) {
+	xlsxFile, error := OpenFile("macExcelTest.xlsx")
+	if error != nil {
+		t.Error(error.Error())
+		return
+	}
+	if xlsxFile == nil {
+		t.Error("OpenFile returned nil FileInterface without generating an os.Error")
+		return
+	}
+	s := xlsxFile.Sheets[0].Cell(0, 0).String()
+	if s != "编号" {
+		t.Errorf("[TestMacExcel] xlsxFile.Sheets[0].Cell(0,0).String():'%s'", s)
+		return
+	}
+}

+ 17 - 1
workbook.go

@@ -5,6 +5,7 @@ import (
 	"encoding/xml"
 	"fmt"
 	"io"
+	"strings"
 )
 
 // xmlxWorkbookRels contains xmlxWorkbookRelations
@@ -133,7 +134,22 @@ func getWorksheetFromSheet(sheet xlsxSheet, worksheets map[string]*zip.File, she
 			sheetName = fmt.Sprintf("sheet%s", sheet.Id)
 		}
 	}
-	f := worksheets[sheetName]
+	f, ok := worksheets[sheetName]
+	if !ok {
+		// excel created from mac MsExcel,will have a sheets Id as "rId1",
+		// but has a sheet file named "sheet1" and  "_rels/sheet1.xml."
+		// this work around will open it
+		for sheetName, f = range worksheets {
+			//do not want "_rels/sheet1.xml."
+			if strings.Contains(sheetName, "_") {
+				continue
+			}
+			break
+		}
+		if f == nil {
+			return nil, fmt.Errorf("not found sheet file sheetId:%s", sheet.Id)
+		}
+	}
 	rc, error = f.Open()
 	if error != nil {
 		return nil, error