0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

cscの作法 その547

Last updated at Posted at 2024-12-28

概要

cscの作法、調べてみた。
xlsxの作成、見つけたので、closedxmlで、やってみた。

参考にしたページ

写真

image.png

サンプルコード


using ClosedXML.Excel;
using System.Data;
using System;
using System.IO;

namespace CreateExcelStream
{
	class Program {
		static void Main(string[] args) {
			var filePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "Booke_s0.xlsx");
			var wb = new XLWorkbook();
			var sheet = wb.Worksheets.Add("Sheet1");
			DataTable table = new DataTable("従業員情報");
			table.Columns.Add("ID", typeof(int));
			table.Columns.Add("名前", typeof(string));
			table.Columns.Add("性別", typeof(string));
			table.Columns.Add("職位", typeof(string));
			table.Columns.Add("部署", typeof(string));
			table.Columns.Add("給与", typeof(decimal));
			table.Columns.Add("入社日", typeof(DateTime));
			table.Rows.Add(1, "佐藤和也", "男", "ソフトウェアエンジニア", "開発部", 750000, DateTime.Parse("2020-03-15"));
			table.Rows.Add(2, "鈴木美咲", "女", "プロジェクトマネージャー", "プロジェクト管理室", 850000, DateTime.Parse("2019-06-22"));
			table.Rows.Add(3, "田中康介", "男", "システムアナリスト", "情報技術部", 800000, DateTime.Parse("2018-07-11"));
			table.Rows.Add(4, "高橋彩香", "女", "UXデザイナー", "デザイン部", 700000, DateTime.Parse("2021-04-05"));
			table.Rows.Add(5, "山本健太", "男", "営業担当", "営業部", 650000, DateTime.Parse("2020-09-10"));
			table.Rows.Add(6, "伊藤真理子", "女", "会計士", "財務部", 720000, DateTime.Parse("2017-11-28"));
			table.Rows.Add(7, "渡辺浩二", "男", "人事マネージャー", "人事部", 820000, DateTime.Parse("2016-05-18"));
			table.Rows.Add(8, "中村菜々子", "女", "法務顧問", "法務部", 900000, DateTime.Parse("2015-02-14"));
			table.Rows.Add(9, "小林翔", "男", "CEO", "最高経営陣", 1200000, DateTime.Parse("2014-08-01"));
			var rangeWithData = sheet.Cell(2, 1).InsertData(table.AsEnumerable());
			sheet.Cell(1, 1).Value = "ID";
			sheet.Cell(1, 2).Value = "名前";
			sheet.Cell(1, 3).Value = "性別";
			sheet.Cell(1, 4).Value = "職位";
			sheet.Cell(1, 5).Value = "部署";
			sheet.Cell(1, 6).Value = "給与";
			sheet.Cell(1, 7).Value = "入社日";
			sheet.Row(1).Style.Fill.BackgroundColor = XLColor.Aqua;
			sheet.Row(1).Style.Font.FontName = "Yu Mincho";
			sheet.Row(1).Style.Font.FontSize = 14;
			sheet.Row(1).Style.Border.OutsideBorder = XLBorderStyleValues.Thin;
			sheet.Row(1).Style.Border.OutsideBorderColor = XLColor.Gray; 
			for (int i = 0; i < table.Rows.Count; i++)
			{
				sheet.Row(i + 2).Style.Fill.BackgroundColor = XLColor.Gray;
				sheet.Row(i + 2).Style.Font.FontName = "Yu Mincho";
				sheet.Row(i + 2).Style.Font.FontSize = 12;
				if ((i + 1) % 2 == 0)
				{
					sheet.Row(i + 2).Style.Fill.BackgroundColor = XLColor.White;
				}
			}
			sheet.Range(2, 1, table.Rows.Count + 1, table.Columns.Count).Style.Border.InsideBorder = XLBorderStyleValues.Thin;
			sheet.Range(2, 1, table.Rows.Count + 1, table.Columns.Count).Style.Border.InsideBorderColor = XLColor.Orange; 
			for (int j = 0; j < table.Columns.Count; j++)
			{
				sheet.Column(j + 1).AdjustToContents();
			}
			wb.SaveAs(filePath);
			Console.WriteLine("ok");
		}
	}
}






以上。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?