stream.go 16.4 KB
Newer Older
1
// Copyright 2016 - 2021 The excelize Authors. All rights reserved. Use of
2 3 4 5
// this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
//
// Package excelize providing a set of functions that allow you to write to
xurime's avatar
xurime 已提交
6
// and read from XLSX / XLSM / XLTM files. Supports reading and writing
xurime's avatar
xurime 已提交
7
// spreadsheet documents generated by Microsoft Excel™ 2007 and later. Supports
xurime's avatar
xurime 已提交
8 9
// complex components by high compatibility, and provided streaming API for
// generating or reading data from a worksheet with huge amounts of data. This
10
// library needs Go version 1.15 or later.
11 12 13 14 15 16 17

package excelize

import (
	"bytes"
	"encoding/xml"
	"fmt"
C
Cameron Howey 已提交
18
	"io"
19 20 21
	"io/ioutil"
	"os"
	"reflect"
C
Cameron Howey 已提交
22 23 24
	"strconv"
	"strings"
	"time"
25 26 27 28
)

// StreamWriter defined the type of stream writer.
type StreamWriter struct {
29 30 31
	File            *File
	Sheet           string
	SheetID         int
32 33
	sheetWritten    bool
	cols            string
34 35 36 37 38
	worksheet       *xlsxWorksheet
	rawData         bufferedWriter
	mergeCellsCount int
	mergeCells      string
	tableParts      string
39 40 41 42 43
}

// NewStreamWriter return stream writer struct by given worksheet name for
// generate new worksheet with large amounts of data. Note that after set
// rows, you must call the 'Flush' method to end the streaming writing
xurime's avatar
xurime 已提交
44
// process and ensure that the order of line numbers is ascending, the common
45 46 47
// API and stream API can't be work mixed to writing data on the worksheets,
// you can't get cell value when in-memory chunks data over 16MB. For
// example, set data for worksheet of size 102400 rows x 50 columns with
xurime's avatar
xurime 已提交
48
// numbers and style:
49 50 51 52
//
//    file := excelize.NewFile()
//    streamWriter, err := file.NewStreamWriter("Sheet1")
//    if err != nil {
xurime's avatar
xurime 已提交
53
//        fmt.Println(err)
54
//    }
xurime's avatar
xurime 已提交
55 56
//    styleID, err := file.NewStyle(`{"font":{"color":"#777777"}}`)
//    if err != nil {
xurime's avatar
xurime 已提交
57
//        fmt.Println(err)
xurime's avatar
xurime 已提交
58
//    }
59 60
//    if err := streamWriter.SetRow("A1", []interface{}{excelize.Cell{StyleID: styleID, Value: "Data"}},
//        excelize.RowOpts{Height: 45, Hidden: false}); err != nil {
xurime's avatar
xurime 已提交
61
//        fmt.Println(err)
xurime's avatar
xurime 已提交
62 63
//    }
//    for rowID := 2; rowID <= 102400; rowID++ {
64 65 66 67 68
//        row := make([]interface{}, 50)
//        for colID := 0; colID < 50; colID++ {
//            row[colID] = rand.Intn(640000)
//        }
//        cell, _ := excelize.CoordinatesToCellName(1, rowID)
xurime's avatar
xurime 已提交
69
//        if err := streamWriter.SetRow(cell, row); err != nil {
xurime's avatar
xurime 已提交
70
//            fmt.Println(err)
71 72 73
//        }
//    }
//    if err := streamWriter.Flush(); err != nil {
xurime's avatar
xurime 已提交
74
//        fmt.Println(err)
75 76
//    }
//    if err := file.SaveAs("Book1.xlsx"); err != nil {
xurime's avatar
xurime 已提交
77
//        fmt.Println(err)
78 79
//    }
//
80 81 82 83 84 85 86
// Set cell value and cell formula for a worksheet with stream writer:
//
//    err := streamWriter.SetRow("A1", []interface{}{
//        excelize.Cell{Value: 1},
//        excelize.Cell{Value: 2},
//        excelize.Cell{Formula: "SUM(A1,B1)"}});
//
87
func (f *File) NewStreamWriter(sheet string) (*StreamWriter, error) {
88
	sheetID := f.getSheetID(sheet)
xurime's avatar
xurime 已提交
89
	if sheetID == -1 {
90 91
		return nil, fmt.Errorf("sheet %s is not exist", sheet)
	}
C
Cameron Howey 已提交
92
	sw := &StreamWriter{
93 94 95 96
		File:    f,
		Sheet:   sheet,
		SheetID: sheetID,
	}
97 98
	var err error
	sw.worksheet, err = f.workSheetReader(sheet)
C
Cameron Howey 已提交
99 100 101
	if err != nil {
		return nil, err
	}
102

103
	sheetPath := f.sheetMap[trimSheetName(sheet)]
104 105 106
	if f.streams == nil {
		f.streams = make(map[string]*StreamWriter)
	}
107
	f.streams[sheetPath] = sw
108

109
	_, _ = sw.rawData.WriteString(XMLHeader + `<worksheet` + templateNamespaceIDMap)
110
	bulkAppendFields(&sw.rawData, sw.worksheet, 2, 5)
111
	return sw, err
112 113
}

C
Cameron Howey 已提交
114 115
// AddTable creates an Excel table for the StreamWriter using the given
// coordinate area and format set. For example, create a table of A1:D5:
116
//
117
//    err := sw.AddTable("A1", "D5", "")
118
//
C
Cameron Howey 已提交
119 120
// Create a table of F2:H6 with format set:
//
121 122 123 124 125 126 127 128
//    err := sw.AddTable("F2", "H6", `{
//        "table_name": "table",
//        "table_style": "TableStyleMedium2",
//        "show_first_column": true,
//        "show_last_column": true,
//        "show_row_stripes": false,
//        "show_column_stripes": true
//    }`)
C
Cameron Howey 已提交
129 130 131 132 133 134 135 136 137 138
//
// Note that the table must be at least two lines including the header. The
// header cells must contain strings and must be unique.
//
// Currently only one table is allowed for a StreamWriter. AddTable must be
// called after the rows are written but before Flush.
//
// See File.AddTable for details on the table format.
func (sw *StreamWriter) AddTable(hcell, vcell, format string) error {
	formatSet, err := parseFormatTableSet(format)
139 140 141
	if err != nil {
		return err
	}
C
Cameron Howey 已提交
142 143 144 145

	coordinates, err := areaRangeToCoordinates(hcell, vcell)
	if err != nil {
		return err
146
	}
xurime's avatar
xurime 已提交
147
	_ = sortCoordinates(coordinates)
C
Cameron Howey 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

	// Correct the minimum number of rows, the table at least two lines.
	if coordinates[1] == coordinates[3] {
		coordinates[3]++
	}

	// Correct table reference coordinate area, such correct C1:B3 to B1:C3.
	ref, err := sw.File.coordinatesToAreaRef(coordinates)
	if err != nil {
		return err
	}

	// create table columns using the first row
	tableHeaders, err := sw.getRowValues(coordinates[1], coordinates[0], coordinates[2])
	if err != nil {
		return err
	}
	tableColumn := make([]*xlsxTableColumn, len(tableHeaders))
	for i, name := range tableHeaders {
		tableColumn[i] = &xlsxTableColumn{
			ID:   i + 1,
			Name: name,
		}
	}

	tableID := sw.File.countTables() + 1

	name := formatSet.TableName
	if name == "" {
		name = "Table" + strconv.Itoa(tableID)
	}

	table := xlsxTable{
181
		XMLNS:       NameSpaceSpreadSheet.Value,
C
Cameron Howey 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
		ID:          tableID,
		Name:        name,
		DisplayName: name,
		Ref:         ref,
		AutoFilter: &xlsxAutoFilter{
			Ref: ref,
		},
		TableColumns: &xlsxTableColumns{
			Count:       len(tableColumn),
			TableColumn: tableColumn,
		},
		TableStyleInfo: &xlsxTableStyleInfo{
			Name:              formatSet.TableStyle,
			ShowFirstColumn:   formatSet.ShowFirstColumn,
			ShowLastColumn:    formatSet.ShowLastColumn,
			ShowRowStripes:    formatSet.ShowRowStripes,
			ShowColumnStripes: formatSet.ShowColumnStripes,
		},
	}

	sheetRelationshipsTableXML := "../tables/table" + strconv.Itoa(tableID) + ".xml"
	tableXML := strings.Replace(sheetRelationshipsTableXML, "..", "xl", -1)

	// Add first table for given sheet.
206
	sheetPath := sw.File.sheetMap[trimSheetName(sw.Sheet)]
C
Cameron Howey 已提交
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
	sheetRels := "xl/worksheets/_rels/" + strings.TrimPrefix(sheetPath, "xl/worksheets/") + ".rels"
	rID := sw.File.addRels(sheetRels, SourceRelationshipTable, sheetRelationshipsTableXML, "")

	sw.tableParts = fmt.Sprintf(`<tableParts count="1"><tablePart r:id="rId%d"></tablePart></tableParts>`, rID)

	sw.File.addContentTypePart(tableID, "table")

	b, _ := xml.Marshal(table)
	sw.File.saveFileList(tableXML, b)
	return nil
}

// Extract values from a row in the StreamWriter.
func (sw *StreamWriter) getRowValues(hrow, hcol, vcol int) (res []string, err error) {
	res = make([]string, vcol-hcol+1)

	r, err := sw.rawData.Reader()
	if err != nil {
		return nil, err
	}

xurime's avatar
xurime 已提交
228
	dec := sw.File.xmlNewDecoder(r)
C
Cameron Howey 已提交
229 230 231 232 233
	for {
		token, err := dec.Token()
		if err == io.EOF {
			return res, nil
		}
234
		if err != nil {
C
Cameron Howey 已提交
235
			return nil, err
236
		}
C
Cameron Howey 已提交
237 238 239
		startElement, ok := getRowElement(token, hrow)
		if !ok {
			continue
240
		}
C
Cameron Howey 已提交
241 242 243 244 245 246 247
		// decode cells
		var row xlsxRow
		if err := dec.DecodeElement(&row, &startElement); err != nil {
			return nil, err
		}
		for _, c := range row.C {
			col, _, err := CellNameToCoordinates(c.R)
248
			if err != nil {
C
Cameron Howey 已提交
249
				return nil, err
250
			}
C
Cameron Howey 已提交
251 252 253 254
			if col < hcol || col > vcol {
				continue
			}
			res[col-hcol] = c.V
255
		}
C
Cameron Howey 已提交
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
		return res, nil
	}
}

// Check if the token is an XLSX row with the matching row number.
func getRowElement(token xml.Token, hrow int) (startElement xml.StartElement, ok bool) {
	startElement, ok = token.(xml.StartElement)
	if !ok {
		return
	}
	ok = startElement.Name.Local == "row"
	if !ok {
		return
	}
	ok = false
	for _, attr := range startElement.Attr {
		if attr.Name.Local != "r" {
			continue
		}
		row, _ := strconv.Atoi(attr.Value)
		if row == hrow {
			ok = true
			return
279 280
		}
	}
C
Cameron Howey 已提交
281
	return
282 283
}

C
Cameron Howey 已提交
284 285 286 287
// Cell can be used directly in StreamWriter.SetRow to specify a style and
// a value.
type Cell struct {
	StyleID int
288
	Formula string
xurime's avatar
xurime 已提交
289
	Value   interface{}
C
Cameron Howey 已提交
290
}
291

292 293 294 295 296 297
// RowOpts define the options for set row.
type RowOpts struct {
	Height float64
	Hidden bool
}

C
Cameron Howey 已提交
298 299 300 301 302 303
// SetRow writes an array to stream rows by giving a worksheet name, starting
// coordinate and a pointer to an array of values. Note that you must call the
// 'Flush' method to end the streaming writing process.
//
// As a special case, if Cell is used as a value, then the Cell.StyleID will be
// applied to that cell.
304
func (sw *StreamWriter) SetRow(axis string, values []interface{}, opts ...RowOpts) error {
C
Cameron Howey 已提交
305
	col, row, err := CellNameToCoordinates(axis)
306 307 308
	if err != nil {
		return err
	}
309 310
	if !sw.sheetWritten {
		if len(sw.cols) > 0 {
311
			_, _ = sw.rawData.WriteString("<cols>" + sw.cols + "</cols>")
312 313 314 315
		}
		_, _ = sw.rawData.WriteString(`<sheetData>`)
		sw.sheetWritten = true
	}
316 317 318 319 320
	attrs, err := marshalRowAttrs(opts...)
	if err != nil {
		return err
	}
	fmt.Fprintf(&sw.rawData, `<row r="%d"%s>`, row, attrs)
C
Cameron Howey 已提交
321 322
	for i, val := range values {
		axis, err := CoordinatesToCellName(col+i, row)
323 324 325
		if err != nil {
			return err
		}
C
Cameron Howey 已提交
326 327 328 329
		c := xlsxC{R: axis}
		if v, ok := val.(Cell); ok {
			c.S = v.StyleID
			val = v.Value
330
			setCellFormula(&c, v.Formula)
C
Cameron Howey 已提交
331 332 333
		} else if v, ok := val.(*Cell); ok && v != nil {
			c.S = v.StyleID
			val = v.Value
334
			setCellFormula(&c, v.Formula)
335
		}
xurime's avatar
xurime 已提交
336
		if err = setCellValFunc(&c, val); err != nil {
337
			_, _ = sw.rawData.WriteString(`</row>`)
338 339
			return err
		}
C
Cameron Howey 已提交
340
		writeCell(&sw.rawData, c)
341
	}
342
	_, _ = sw.rawData.WriteString(`</row>`)
C
Cameron Howey 已提交
343 344
	return sw.rawData.Sync()
}
345

346 347 348
// marshalRowAttrs prepare attributes of the row by given options.
func marshalRowAttrs(opts ...RowOpts) (attrs string, err error) {
	var opt *RowOpts
xurime's avatar
xurime 已提交
349 350
	for i := range opts {
		opt = &opts[i]
351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367
	}
	if opt == nil {
		return
	}
	if opt.Height > MaxRowHeight {
		err = ErrMaxRowHeight
		return
	}
	if opt.Height > 0 {
		attrs += fmt.Sprintf(` ht="%v" customHeight="true"`, opt.Height)
	}
	if opt.Hidden {
		attrs += ` hidden="true"`
	}
	return
}

368
// SetColWidth provides a function to set the width of a single column or
369
// multiple columns for the StreamWriter. Note that you must call
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
// the 'SetColWidth' function before the 'SetRow' function. For example set
// the width column B:C as 20:
//
//    err := streamWriter.SetColWidth(2, 3, 20)
//
func (sw *StreamWriter) SetColWidth(min, max int, width float64) error {
	if sw.sheetWritten {
		return ErrStreamSetColWidth
	}
	if min > TotalColumns || max > TotalColumns {
		return ErrColumnNumber
	}
	if min < 1 || max < 1 {
		return ErrColumnNumber
	}
	if width > MaxColumnWidth {
		return ErrColumnWidth
	}
	if min > max {
		min, max = max, min
	}
	sw.cols += fmt.Sprintf(`<col min="%d" max="%d" width="%f" customWidth="1"/>`, min, max, width)
	return nil
}

395 396 397 398 399 400 401 402 403 404 405 406 407
// MergeCell provides a function to merge cells by a given coordinate area for
// the StreamWriter. Don't create a merged cell that overlaps with another
// existing merged cell.
func (sw *StreamWriter) MergeCell(hcell, vcell string) error {
	_, err := areaRangeToCoordinates(hcell, vcell)
	if err != nil {
		return err
	}
	sw.mergeCellsCount++
	sw.mergeCells += fmt.Sprintf(`<mergeCell ref="%s:%s"/>`, hcell, vcell)
	return nil
}

408 409 410 411 412 413 414
// setCellFormula provides a function to set formula of a cell.
func setCellFormula(c *xlsxC, formula string) {
	if formula != "" {
		c.F = &xlsxF{Content: formula}
	}
}

xurime's avatar
xurime 已提交
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
// setCellValFunc provides a function to set value of a cell.
func setCellValFunc(c *xlsxC, val interface{}) (err error) {
	switch val := val.(type) {
	case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
		err = setCellIntFunc(c, val)
	case float32:
		c.T, c.V = setCellFloat(float64(val), -1, 32)
	case float64:
		c.T, c.V = setCellFloat(val, -1, 64)
	case string:
		c.T, c.V, c.XMLSpace = setCellStr(val)
	case []byte:
		c.T, c.V, c.XMLSpace = setCellStr(string(val))
	case time.Duration:
		c.T, c.V = setCellDuration(val)
	case time.Time:
		c.T, c.V, _, err = setCellTime(val)
	case bool:
		c.T, c.V = setCellBool(val)
	case nil:
		c.T, c.V, c.XMLSpace = setCellStr("")
	default:
		c.T, c.V, c.XMLSpace = setCellStr(fmt.Sprint(val))
	}
	return err
}

// setCellIntFunc is a wrapper of SetCellInt.
func setCellIntFunc(c *xlsxC, val interface{}) (err error) {
	switch val := val.(type) {
	case int:
		c.T, c.V = setCellInt(val)
	case int8:
		c.T, c.V = setCellInt(int(val))
	case int16:
		c.T, c.V = setCellInt(int(val))
	case int32:
		c.T, c.V = setCellInt(int(val))
	case int64:
		c.T, c.V = setCellInt(int(val))
	case uint:
		c.T, c.V = setCellInt(int(val))
	case uint8:
		c.T, c.V = setCellInt(int(val))
	case uint16:
		c.T, c.V = setCellInt(int(val))
	case uint32:
		c.T, c.V = setCellInt(int(val))
	case uint64:
		c.T, c.V = setCellInt(int(val))
	default:
	}
	return
}

C
Cameron Howey 已提交
470
func writeCell(buf *bufferedWriter, c xlsxC) {
471
	_, _ = buf.WriteString(`<c`)
C
Cameron Howey 已提交
472 473
	if c.XMLSpace.Value != "" {
		fmt.Fprintf(buf, ` xml:%s="%s"`, c.XMLSpace.Name.Local, c.XMLSpace.Value)
474
	}
C
Cameron Howey 已提交
475 476 477 478 479 480 481
	fmt.Fprintf(buf, ` r="%s"`, c.R)
	if c.S != 0 {
		fmt.Fprintf(buf, ` s="%d"`, c.S)
	}
	if c.T != "" {
		fmt.Fprintf(buf, ` t="%s"`, c.T)
	}
482
	_, _ = buf.WriteString(`>`)
483 484 485 486 487
	if c.F != nil {
		_, _ = buf.WriteString(`<f>`)
		_ = xml.EscapeText(buf, []byte(c.F.Content))
		_, _ = buf.WriteString(`</f>`)
	}
C
Cameron Howey 已提交
488
	if c.V != "" {
489 490 491
		_, _ = buf.WriteString(`<v>`)
		_ = xml.EscapeText(buf, []byte(c.V))
		_, _ = buf.WriteString(`</v>`)
C
Cameron Howey 已提交
492
	}
493
	_, _ = buf.WriteString(`</c>`)
494 495
}

C
Cameron Howey 已提交
496 497
// Flush ending the streaming writing process.
func (sw *StreamWriter) Flush() error {
498 499 500 501
	if !sw.sheetWritten {
		_, _ = sw.rawData.WriteString(`<sheetData>`)
		sw.sheetWritten = true
	}
502
	_, _ = sw.rawData.WriteString(`</sheetData>`)
503 504 505 506 507 508
	bulkAppendFields(&sw.rawData, sw.worksheet, 8, 15)
	if sw.mergeCellsCount > 0 {
		sw.mergeCells = fmt.Sprintf(`<mergeCells count="%d">%s</mergeCells>`, sw.mergeCellsCount, sw.mergeCells)
	}
	_, _ = sw.rawData.WriteString(sw.mergeCells)
	bulkAppendFields(&sw.rawData, sw.worksheet, 17, 38)
509
	_, _ = sw.rawData.WriteString(sw.tableParts)
xurime's avatar
xurime 已提交
510
	bulkAppendFields(&sw.rawData, sw.worksheet, 40, 40)
511
	_, _ = sw.rawData.WriteString(`</worksheet>`)
C
Cameron Howey 已提交
512 513 514 515
	if err := sw.rawData.Flush(); err != nil {
		return err
	}

516
	sheetPath := sw.File.sheetMap[trimSheetName(sw.Sheet)]
517
	sw.File.Sheet.Delete(sheetPath)
518
	delete(sw.File.checked, sheetPath)
519
	sw.File.Pkg.Delete(sheetPath)
C
Cameron Howey 已提交
520 521

	return nil
522 523
}

524 525 526
// bulkAppendFields bulk-appends fields in a worksheet by specified field
// names order range.
func bulkAppendFields(w io.Writer, ws *xlsxWorksheet, from, to int) {
527
	s := reflect.ValueOf(ws).Elem()
C
Cameron Howey 已提交
528
	enc := xml.NewEncoder(w)
529
	for i := 0; i < s.NumField(); i++ {
530
		if from <= i && i <= to {
531
			_ = enc.Encode(s.Field(i).Interface())
532 533 534 535
		}
	}
}

C
Cameron Howey 已提交
536 537
// bufferedWriter uses a temp file to store an extended buffer. Writes are
// always made to an in-memory buffer, which will always succeed. The buffer
xurime's avatar
xurime 已提交
538 539
// is written to the temp file with Sync, which may return an error.
// Therefore, Sync should be periodically called and the error checked.
C
Cameron Howey 已提交
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
type bufferedWriter struct {
	tmp *os.File
	buf bytes.Buffer
}

// Write to the in-memory buffer. The err is always nil.
func (bw *bufferedWriter) Write(p []byte) (n int, err error) {
	return bw.buf.Write(p)
}

// WriteString wites to the in-memory buffer. The err is always nil.
func (bw *bufferedWriter) WriteString(p string) (n int, err error) {
	return bw.buf.WriteString(p)
}

// Reader provides read-access to the underlying buffer/file.
func (bw *bufferedWriter) Reader() (io.Reader, error) {
	if bw.tmp == nil {
		return bytes.NewReader(bw.buf.Bytes()), nil
	}
	if err := bw.Flush(); err != nil {
		return nil, err
	}
	fi, err := bw.tmp.Stat()
	if err != nil {
		return nil, err
566
	}
C
Cameron Howey 已提交
567 568 569 570
	// os.File.ReadAt does not affect the cursor position and is safe to use here
	return io.NewSectionReader(bw.tmp, 0, fi.Size()), nil
}

xurime's avatar
xurime 已提交
571 572
// Sync will write the in-memory buffer to a temp file, if the in-memory
// buffer has grown large enough. Any error will be returned.
C
Cameron Howey 已提交
573 574
func (bw *bufferedWriter) Sync() (err error) {
	// Try to use local storage
575
	if bw.buf.Len() < StreamChunkSize {
C
Cameron Howey 已提交
576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
		return nil
	}
	if bw.tmp == nil {
		bw.tmp, err = ioutil.TempFile(os.TempDir(), "excelize-")
		if err != nil {
			// can not use local storage
			return nil
		}
	}
	return bw.Flush()
}

// Flush the entire in-memory buffer to the temp file, if a temp file is being
// used.
func (bw *bufferedWriter) Flush() error {
	if bw.tmp == nil {
		return nil
	}
	_, err := bw.buf.WriteTo(bw.tmp)
	if err != nil {
		return err
	}
	bw.buf.Reset()
	return nil
}

// Close the underlying temp file and reset the in-memory buffer.
func (bw *bufferedWriter) Close() error {
	bw.buf.Reset()
	if bw.tmp == nil {
		return nil
607
	}
C
Cameron Howey 已提交
608 609
	defer os.Remove(bw.tmp.Name())
	return bw.tmp.Close()
610
}