4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

 C# Excel文書を作成

Last updated at Posted at 2021-05-19

今回は C#ではSpire.XLSという無料のライブラリを活用して、Excel文書を作成する方法を紹介してさしあげましょう。

下準備

1.E-iceblueの公式サイトからFree Spire.XLS無料版をダウンロードしてください。

 f:id:lendoris:20210519144411p:plain

2.Visual Studioを起動して新規プロジェクトを作成してから、インストールされたファイルにあった相応しいSpire.XLS.dllを参照に追加してください。

(Net 4.0を例としたら、デフォルトパスは“Bin→NET4.0→XLS.dll”というようです。)

f:id:lendoris:20210519144429p:plain

```C# using Spire.Xls; using System.Data; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { //workbook objectを作成します。 Workbook wb = new Workbook();
        //既定のシートを削除します。
        wb.Worksheets.Clear();

        //新規シートを追加し、それに名をつけます。

        Worksheet sheet = wb.Worksheets.Add("職員名簿");

        //A1からG1までのセルを横方向に結合します。
        sheet.Range["A1:G1"].Merge();

        // A1セルにデータを書き込み、文字の書式などを設定します。
        sheet.Range["A1"].Value = "パンダ会社職員個人情報リスト";
        sheet.Range["A1"].HorizontalAlignment = HorizontalAlignType.Center;
        sheet.Range["A1"].VerticalAlignment = VerticalAlignType.Center;
        sheet.Range["A1"].Style.Font.IsBold = true;
        sheet.Range["A1"].Style.Font.Size = 13F;

        //初めの行の高さを設定します。
        sheet.Rows[0].RowHeight = 30F;

        //创建一个DataTable
        DataTable dt = new DataTable();
        dt.Columns.Add("名前");
        dt.Columns.Add("性别");
        dt.Columns.Add("出生年月");
        dt.Columns.Add("学歴");
        dt.Columns.Add("電話番号");
        dt.Columns.Add("職務");
        dt.Columns.Add("職番号");
        dt.Rows.Add("佐藤翔太", "男", "1990年2月10日", "学士", " (090)1234-5678 ", "運転士", "0054");
        dt.Rows.Add("田中樹", "男", "1985年6月8日", "学士", " (090)1234-5678 ", "教師", "0055");
        dt.Rows.Add("伊藤エミリ", "女", "1989年11月25日", "学士", " (090)1234-5678 ", "医者", "0029");
        dt.Rows.Add("鈴木隼人", "男", "1978年4月16日", "修士", " (090)1234-5678 ", "警察", "0036");
        dt.Rows.Add("工藤晴子", "女", "1980年1月21日", "学士", " (090)1234-5678 ", "教師", "0010");

        //DataTableのデータをシートに追加します。
        sheet.InsertDataTable(dt, true, 2, 1, true);

        //このエリアの行の高さを設定します。
        sheet.Range["A2:G7"].RowHeight = 15F;

        //三つ目と五つ目の列の幅を設定しあmす。
        sheet.Range["A2:G7"].Columns[2].ColumnWidth = 15F;
        sheet.Range["A2:G7"].Columns[4].ColumnWidth = 15F;

        //グリッドのスタイルを設定します。
        sheet.Range["A2:G7"].BorderAround(LineStyleType.Medium);
        sheet.Range["A2:G7"].BorderInside(LineStyleType.Thin);
        sheet.Range["A2:G2"].BorderAround(LineStyleType.Medium);
        sheet.Range["A2:G7"].Borders.KnownColor = ExcelColors.Black;

        //.xlsxで保存します。
        wb.SaveToFile("Excel.xlsx", FileFormat.Version2013);



    }
}

}



<h4><strong>実行結果</strong></h4>
<p><img src="https://cdn-ak.f.st-hatena.com/images/fotolife/l/lendoris/20210519/20210519144537.png" alt="f:id:lendoris:20210519144537p:plain" title="" class="hatena-fotolife" itemprop="image" /></p>
<p><strong> </strong></p>
4
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?