Skip to content

Commit

Permalink
Merge pull request #62 from devlights/add-ExportAsFixedFormat-func
Browse files Browse the repository at this point in the history
Add Worksheet.ExportAsFixedFormat
  • Loading branch information
devlights committed Aug 3, 2021
2 parents c3fc75d + a9e5c72 commit 69ac57b
Show file tree
Hide file tree
Showing 6 changed files with 164 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,5 @@ fabric.properties

# Android studio 3.1+ serialized cache file
.idea/caches/build_file_checksums.ser

.idea/
17 changes: 17 additions & 0 deletions constants/xlFixedFormatType.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package constants

type (
// XlFixedFormatType -- ファイル形式の種類を指定します。
XlFixedFormatType int
)

// XlFixedFormatType -- ファイル形式の種類を指定します。
//
// REFERENCES:
// - https://docs.microsoft.com/ja-jp/office/vba/api/excel.xlfixedformattype
//
//noinspection GoUnusedConst
const (
XlTypePDF XlFixedFormatType = 0 // PDF
XlTypeXPS XlFixedFormatType = 1 // XPS
)
8 changes: 8 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ This directory contains following examples.

- copy_sheet
- Example of Worksheet.CopySheet
- export_pdf
- Example of Worksheet.ExportAsFixedFormat
- font_and_interior
- Example of Cell.Font and Cell.Interior
- helloworld
Expand Down Expand Up @@ -41,6 +43,12 @@ $ go run github.com/devlights/goxcel/examples/copy_sheet -srcdir path/to/src/exc

After processing, a file named result.xlsx is generated in the current directory and contains all the sheets in the Excel file under the directory specified by the parameter.

- export_pdf

```sh
$ go run github.com/devlights/goxcel/examples/export_pdf -src /path/to/src/excel/file -dst /path/to/dest/pdf/file
```

- font_and_interior

```shell script
Expand Down
89 changes: 89 additions & 0 deletions examples/export_pdf/export_pdf.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package main

import (
"flag"
"fmt"
"log"
"os"
"path/filepath"

"github.com/devlights/goxcel"
"github.com/devlights/goxcel/constants"
)

// flag parameters
var (
src string
dst string
)

// logs
var (
appLog = log.New(os.Stdout, ">>> ", 0)
)

func main() {
var (
returnCode int
)

if err := run(); err != nil {
_, _ = fmt.Fprint(os.Stderr, err)
returnCode = -1
}

appLog.Println("done")

os.Exit(returnCode)
}

func run() error {
abs := func(p string) string {
v, _ := filepath.Abs(p)
return v
}

flag.StringVar(&src, "src", "", "source file")
flag.StringVar(&dst, "dst", "result.pdf", "output pdf name")
flag.Parse()

if src == "" {
flag.Usage()
return nil
}

quitFn, _ := goxcel.InitGoxcel()
defer quitFn()

g, r, _ := goxcel.NewGoxcel()
defer r()

_ = g.Silent(false)

wbs, err := g.Workbooks()
if err != nil {
return err
}

absPath := abs(src)
wb, wbReleaseFn, err := wbs.Open(abs(src))
if err != nil {
return err
}
defer wbReleaseFn()
appLog.Printf("WorkBook Open: %s", absPath)

ws, err := wb.Sheets(1)
if err != nil {
return err
}

absPath = abs(src)
err = ws.ExportAsFixedFormat(constants.XlTypePDF, absPath)
if err != nil {
return err
}
appLog.Printf("Export PDF : %s", absPath)

return nil
}
6 changes: 6 additions & 0 deletions worksheet.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package goxcel
import (
"fmt"

"github.com/devlights/goxcel/constants"
"github.com/go-ole/go-ole"
"github.com/go-ole/go-ole/oleutil"
)
Expand Down Expand Up @@ -174,3 +175,8 @@ func (ws *Worksheet) CopySheet(dest *Worksheet, after bool) error {

return e
}

func (ws *Worksheet) ExportAsFixedFormat(fmtType constants.XlFixedFormatType, path string) error {
_, err := oleutil.CallMethod(ws.ComObject(), "ExportAsFixedFormat", int(fmtType), path)
return err
}
42 changes: 42 additions & 0 deletions worksheet_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package goxcel

import (
"os"
"path/filepath"
"testing"

"github.com/devlights/goxcel/constants"
"github.com/devlights/goxcel/testutil"
)

Expand Down Expand Up @@ -123,3 +126,42 @@ func TestWorksheet_HPageBreaks(t *testing.T) {
testutil.Interval()
testutil.Interval()
}

func TestWorksheet_ExportAsFixedFormat(t *testing.T) {
testutil.Interval()
defer testutil.Interval()

// Arrange
g, r, _ := NewGoxcel()
defer r()

_ = g.SetDisplayAlerts(false)
_ = g.SetVisible(false)

wbs, _ := g.Workbooks()
wb, wbReleaseFn, _ := wbs.Add()
defer wbReleaseFn()

ws, _ := wb.Sheets(1)
c, _ := ws.Cells(10, 1)
_ = c.SetValue("helloworld")

// Act
p := filepath.Join(os.TempDir(), "goxcel-ExportAsFixedFormat-test.pdf")
t.Log(p)

err := ws.ExportAsFixedFormat(constants.XlTypePDF, p)
if err != nil {
t.Error(err)
}

// Assert
_, err = os.Stat(p)
if err != nil {
t.Errorf("[want] file exists\t[got] file not exists\t%s", p)
}

_ = g.SetVisible(true)
testutil.Interval()
testutil.Interval()
}

0 comments on commit 69ac57b

Please sign in to comment.