csharp-excel

IWorkbook workbook = new XSSFWorkbook();
ISheet sheet1 = workbook.CreateSheet("data");

// 填写标题 固定部分
var row = sheet1.CreateRow(0);
iColIdx = 0;
for (; iColIdx < titls.Length; ++iColIdx)
{
var cell1 = row.CreateCell(iColIdx);
cell1.SetCellType(CellType.Blank);
cell1.SetCellValue(titls[iColIdx]);
}
// 数据行
var rowData = sheet1.CreateRow(j);

设置文本格式

IDataFormat dataformat = workbook.CreateDataFormat();
ICellStyle styleText = workbook.CreateCellStyle();
styleText.DataFormat = dataformat.GetFormat("@");//文本格式

//时间 必须按照格式:year-mon-day HH:MM:SS 如 2020-07-01 10:10:10
var cell1 = rowData.CreateCell(iColIdx);
cell1.SetCellType(CellType.String);
cell1.CellStyle = styleText;
cell1.SetCellValue(strDate);