github.com/keysonzzz/kmg@v0.0.0-20151121023212-05317bfd7d39/encoding/kmgExcel/XlsxFile2Array.go (about)

     1  package kmgExcel
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/tealeg/xlsx"
     7  	//"io"
     8  	//"archive/zip"
     9  )
    10  
    11  // get all raw data from excel
    12  // output index mean=> sheetIndex ,row ,cell ,value
    13  // not remove any cells
    14  func XlsxFile2Array(path string) ([][][]string, error) {
    15  	file, err := xlsx.OpenFile(path)
    16  	if err != nil {
    17  		return nil, err
    18  	}
    19  	return xlsx2ArrayXlsxFile(file)
    20  }
    21  
    22  //get raw data from one sheet of excel
    23  //output index mean=> row ,cell ,value
    24  // not remove any cells
    25  func XlsxFileSheetIndex2Array(path string, index int) ([][]string, error) {
    26  	file, err := xlsx.OpenFile(path)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	output := [][]string{}
    31  	sheet := file.Sheets[index]
    32  	for _, row := range sheet.Rows {
    33  		if row == nil {
    34  			continue
    35  		}
    36  		r := []string{}
    37  		for _, cell := range row.Cells {
    38  			r = append(r, cell.String())
    39  		}
    40  		output = append(output, r)
    41  	}
    42  	return output, nil
    43  }
    44  
    45  //get data from first sheet of excel
    46  //output index mean=> row ,cell ,value
    47  // remove all right and bottom blank cells
    48  func XlsxFileFirstSheet2ArrayTrim(path string) (output [][]string, err error) {
    49  	output, err = XlsxFileSheetIndex2Array(path, 0)
    50  	if err != nil {
    51  		return
    52  	}
    53  	output = Trim2DArray(output)
    54  	return
    55  }
    56  
    57  //get data from first column of first sheet of excel file
    58  //output index name=>row index,value
    59  //remove any blank cells.
    60  // output will not include blank cell in middle.
    61  func XlsxFileFirstSheet2FirstColumnTrim(path string) (output []string, err error) {
    62  	outputArray, err := XlsxFileFirstSheet2ArrayTrim(path)
    63  	if err != nil {
    64  		return
    65  	}
    66  	output = make([]string, len(outputArray))
    67  	i := 0
    68  	for _, row := range outputArray {
    69  		v := strings.TrimSpace(row[0])
    70  		if v == "" {
    71  			continue
    72  		}
    73  		output[i] = v
    74  		i++
    75  	}
    76  	output = output[:i]
    77  	return
    78  }
    79  
    80  func xlsx2ArrayXlsxFile(file *xlsx.File) (output [][][]string, err error) {
    81  	output = [][][]string{}
    82  	for _, sheet := range file.Sheets {
    83  		s := [][]string{}
    84  		for _, row := range sheet.Rows {
    85  			if row == nil {
    86  				continue
    87  			}
    88  			r := []string{}
    89  			for _, cell := range row.Cells {
    90  				r = append(r, cell.String())
    91  			}
    92  			s = append(s, r)
    93  		}
    94  		output = append(output, s)
    95  	}
    96  	return output, nil
    97  }