彷徨えるサラリーマン

彷徨えるサラリーマン

グチやら、ニュースへの突っ込みやら、勝手気ままに書いてま~す。

Amebaでブログを始めよう!

tealeg/xlsxライブラリを使ってExcelを読み込むとき、

大抵のサイトの説明では、サンプルを引用している。

 

例えば、こんなところ

http://pineplanter.moo.jp/non-it-salaryman/2017/06/20/go-read-excel2/

や、こんなところ

https://qiita.com/from_kyushu/items/cf3b4516bb0ab2e830bc

 

ほかにもいろいろあったけど、まあだいたい同じような感じ

そのソースはこちら

package main
import (
        "fmt"
        "github.com/tealeg/xlsx"
)
func main() {
        excelFileName := "/home/tealeg/foo.xlsx"
        xlFile, err := xlsx.OpenFile(excelFileName)
        if err != nil {
                ...
        }
        for _, sheet := range xlFile.Sheets {
                for _, row := range sheet.Rows {
                        for _, cell := range row.Cells {
                                text, _ := cell.String()
                                fmt.Printf("%s\n", text)
                        }
                }
        }
}

これを参考に作ってみたけど、ビルドでエラーになる

# command-line-arguments

,プロpグラム名:行:カラム sheet1.Rows undefined (type *xlsx.Sheet has no field or method Rows)

 

要するに、Rowsというのは、フィールドにも定義されてないし、メソッドもないよ・・・

と言っている。

この説明が書かれたのは2017年でもう3年以上たっているので、変更されたのかと思って、sheet.goを見ると・・・

 

type Sheet struct {
    Name              string
    File                 *File
    Cols                *ColStore
    MaxRow           int
    MaxCol            int
    Hidden             bool
    Selected          bool
    SheetViews      []SheetView
    SheetFormat    SheetFormat
    AutoFilter        *AutoFilter
    Relations         []Relation
    DataValidations []*xlsxDataValidation
    cellStore          CellStore
    currentRow      *Row
}

確かにRowsなんてない・・・
ということで、MaxrowとMaxColを使って、ゴリゴリ回してみた。
それがこちら
package main

import (
    "fmt"
    "log"

    "github.com/tealeg/xlsx"
)

func main() {
    file, err := xlsx.OpenFile("test.xlsx")
    if err != nil {
        log.Fatal(err)
    }
    for _, sheet := range file.Sheets {
        for row := 0 ; row < sheet.MaxRow ;row++ {
            for col := 0 ; col < sheet.MaxCol ;col++ {
                v, err1 := sheet.Cell(row,col)
                if err1 != nil {
                    log.Fatal(err1)
                }
                fmt.Println(v)
            }
        }
    }
}
一応これで実行したら、ちゃんと動いたので、とりあえず記録しておくことにした。
 

 

PHPのインストール

# yum -y install php

 

インストール確認

/etc/httpd/conf.d/ の中に php.conf があればよい。

 

localhostにアクセスした時にphpinfoを表示する。

/etc/httpd/conf/httpd.conf の

以下の部分

   <IfModule dir_module>
       DirectoryIndex index.html index.php ←追加
   </IfModule>

/var/www/html/ に index.php を作成

その中身

   <?php
   phpinfo();
   ?>

にして、ブラウザから

  http://localhost

でphp情報が出ればOK

Sub Seikei1()
'
' Macro1 Seikei1
' ①格子状の罫線を引く
' ②表題の色を変える
' ③折り返して表示するを解除して、自動で列と行の幅を設定する
'
Dim LastRow As Long
Dim LastCol As Long
Dim i As Long
    LastRow = Cells(Rows.Count, 1).End(xlUp).Row  '最終行を求める
    LastCol = Cells(1, Columns.Count).End(xlToLeft).Column  '最終列を求める
    Range(Cells(1, 1), Cells(1, LastCol)).Interior.ColorIndex = 35 '黄緑にする
    Cells.WrapText = False  '「折り返して表示する」を解除する
   
    '格子状に罫線を引く
    Range(Cells(1, 1), Cells(LastRow, LastCol)).Select
    Selection.Borders.LineStyle = xlContinuous
   
    '列の幅を自動で調節する
    For i = 1 To LastCol
        Columns(i).EntireColumn.AutoFit
    Next i
   
    '行の幅を自動で調節する
    For i = 1 To LastRow
        Rows(i).EntireRow.AutoFit
    Next i
End Sub