1.首先须要有一个NPOI
2.接下来上代码
private void button1_Click(object sender, EventArgs e)
{
//1.通过Ado.net读取数据
string sql = "SELECT * FROM ASRS_F1";
using (SqlDataReader reader = SqlHelper.ExecuteReader(sql, CommandType.Text))
{
//如果读到数据
if (reader.HasRows)
{
IWorkbook wk = new HSSFWorkbook();
ISheet sheet = wk.CreateSheet("ASRS_F1"); #region 创建第一行,设置列名
//--------------------------------------------------
//创建第一行,第一行表示列名
IRow rowHead = sheet.CreateRow();
//循环查询出的每一列
for (int col = ; col < reader.FieldCount; col++)
{
rowHead.CreateCell(col).SetCellValue(reader.GetName(col));
}
//--------------------------------------------------
#endregion int rindex = ;
//下面是创建数据行
while (reader.Read())
{
//ID, Position, AName, ACode, AState, ABatch, ADateTime, BName, BCode, BState, BBatch, BDateTime, IsMoving, IsType
IRow currentRow = sheet.CreateRow(rindex);
rindex++;
int Id = reader.GetInt32();
string Position = reader.GetString();
string AName = reader.GetString();
string ACode = reader.GetString();
string AState = reader.GetString();
string ABatch =reader.IsDBNull()?null: reader.GetString();
DateTime? lockDate = reader.IsDBNull() ? null : (DateTime?)reader.GetDateTime();
string BName = reader.GetString();
string BCode = reader.GetString();
string BState = reader.GetString();
string BBatch = reader.IsDBNull()?null:reader.GetString();
DateTime? lockDates = reader.IsDBNull() ? null : (DateTime?)reader.GetDateTime();
string IsMoving = reader.GetString();
string IsType = reader.GetString(); currentRow.CreateCell().SetCellValue(Id);
currentRow.CreateCell().SetCellValue(Position);
currentRow.CreateCell().SetCellValue(AName);
currentRow.CreateCell().SetCellValue(ACode);
currentRow.CreateCell().SetCellValue(AState);
currentRow.CreateCell().SetCellValue(ABatch);
if (lockDate == null)
{
//如果是null值,那么就像excel写入一个单元格,这个单元格的类型就是Blank
currentRow.CreateCell().SetCellType(CellType.BLANK);
}
else
{ //创建一个单元格
ICell cellLockDate = currentRow.CreateCell(); //创建一个单元格样式
ICellStyle cellStyle = wk.CreateCellStyle();
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("m/d/yy h:mm");
//设置当前单元格应用cellStyle样式
cellLockDate.CellStyle = cellStyle; cellLockDate.SetCellValue((DateTime)lockDate);
}
currentRow.CreateCell().SetCellValue(BName);
currentRow.CreateCell().SetCellValue(BCode);
currentRow.CreateCell().SetCellValue(BState);
currentRow.CreateCell().SetCellValue(BBatch); if (lockDates == null)
{
//如果是null值,那么就像excel写入一个单元格,这个单元格的类型就是Blank
currentRow.CreateCell().SetCellType(CellType.BLANK);
}
else
{ //创建一个单元格
ICell cellLockDate = currentRow.CreateCell(); //创建一个单元格样式
ICellStyle cellStyle = wk.CreateCellStyle();
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("m/d/yy h:mm");
//设置当前单元格应用cellStyle样式
cellLockDate.CellStyle = cellStyle; cellLockDate.SetCellValue((DateTime)lockDates);
}
currentRow.CreateCell().SetCellValue(IsMoving);
currentRow.CreateCell().SetCellValue(IsType);
} //写入
using (FileStream fsWrite = File.OpenWrite("半成品库存表.xls"))
{
wk.Write(fsWrite);
}
label1.Text = "写入成功!" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
}
else
{
label1.Text = "没有查询到任何数据";
}
} }