using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IO;
namespace ExcelNpoi
{
class Program
{
static void Main(string[] args)
{
String filepath = @"c:\output\npoi.xlsx";
IWorkbook workbook = new XSSFWorkbook();
ISheet sheet = workbook.CreateSheet("mySheet");
// 日付の値設定
IRow row = sheet.CreateRow(2);
ICellStyle dateStyle = workbook.CreateCellStyle();
dateStyle.DataFormat = workbook.CreateDataFormat().GetFormat("yyyy/mm/dd");
ICell cell = row.CreateCell(1);
cell.SetCellType(CellType.Numeric);
cell.CellStyle = dateStyle;
cell.SetCellValue("2019/9/4");
cell = row.CreateCell(2);
cell.SetCellType(CellType.String);
cell.CellStyle = dateStyle;
cell.SetCellValue("2019/9/5");
cell = row.CreateCell(3);
cell.SetCellType(CellType.Numeric);
cell.CellStyle = dateStyle;
cell.SetCellValue(DateTime.Parse("2019/9/6"));
// 時刻の値設定
row = sheet.CreateRow(3);
ICellStyle timeStyle = workbook.CreateCellStyle();
timeStyle.DataFormat = workbook.CreateDataFormat().GetFormat("HH:mm:ss");
cell = row.CreateCell(1);
cell.SetCellType(CellType.Numeric);
cell.CellStyle = timeStyle;
cell.SetCellValue("3:16:3");
cell = row.CreateCell(2);
cell.SetCellType(CellType.String);
cell.CellStyle = timeStyle;
cell.SetCellValue("3:16:3");
cell = row.CreateCell(3);
cell.SetCellType(CellType.Numeric);
cell.CellStyle = timeStyle;
cell.SetCellValue(DateTime.Parse("3:16:4"));
using (FileStream fs = File.Create(filepath))
{
workbook.Write(fs);
}
}
}
}