直接上代码吧:简单易懂
/// <summary>
/// 通过NPOI导出excel文件
/// </summary>
/// <param name="sFileFullName">excel文件全路径</param>
/// <returns>是否导出成功(0:导出成功,其他:导出不成功)</returns>
private int OutputExcelByNPOI(string sFileFullName)
{
//1.创建sheet页
HSSFWorkbook workbook = new HSSFWorkbook();
ISheet sheet = workbook.CreateSheet("sheet1");
//2.创建样式
//左对齐样式
ICellStyle cLeftLoakStyle = workbook.CreateCellStyle();
cLeftLoakStyle.IsLocked = true;
cLeftLoakStyle.WrapText = true;
cLeftLoakStyle.Alignment = HorizontalAlignment.Left;
cLeftLoakStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
//右对齐样式
ICellStyle cRightLoakStyle = workbook.CreateCellStyle();
cRightLoakStyle.CloneStyleFrom(cLeftLoakStyle);
cRightLoakStyle.Alignment = HorizontalAlignment.Right;
//不锁定的样式(左对齐):
ICellStyle cUnLoakStyle = workbook.CreateCellStyle();
cUnLoakStyle.CloneStyleFrom(cRightLoakStyle);
cUnLoakStyle.IsLocked = false;
//3.设置具体数据
#region Part-I
SetCellValue(sheet, 0, 0, "PartⅠ-Title_1 ", cLeftLoakStyle);
int nCount = data.Count;
int nCurrentRowIndx = 0;
for (int i = 0; i < nCount; i++)
{
Kpidata data = kpidata[i];
SetCellValue(sheet, ++nCurrentRowIndx, 1, string.Format("KPI-{0}", i + 1), cRightLoakStyle);
SetCellValue(sheet, ++nCurrentRowIndx, 2, "Latitude:", cRightLoakStyle);
SetCellValue(sheet, nCurrentRowIndx, 3, data.Latitude, cLeftLoakStyle);
SetCellValue(sheet, nCurrentRowIndx, 4, "Weights:", cRightLoakStyle);
SetCellValue(sheet, nCurrentRowIndx, 5, string.Format("{0}%", data.Weights), cLeftLoakStyle);
SetCellValue(sheet, nCurrentRowIndx, 6, "DataSources:", cRightLoakStyle);
SetCellValue(sheet, nCurrentRowIndx, 7, data.DataSources, cLeftLoakStyle);
SetCellValue(sheet, ++nCurrentRowIndx, 2, "TargetValue:", cRightLoakStyle);
SetCellValue(sheet, nCurrentRowIndx, 3, data.TargetValue, cLeftLoakStyle);
SetCellValue(sheet, ++nCurrentRowIndx, 2, "EmployeeSummary :", cRightLoakStyle);
SetCellValue(sheet, nCurrentRowIndx, 3, data.EmployeeSummary, cUnLoakStyle);
}
//4.设置自动列宽
for (int i = 0; i < 8; i++)
{
sheet.AutoSizeColumn(i);
}
//5.设置sheet页保护
sheet.ProtectSheet("");
//6.本地保存文件
FileStream fs = new FileStream(sFileFullName, FileMode.CreateNew);
workbook.Write(fs);
fs.Close();
return 0;
}
#endregion
/// <summary>
/// 设置exce单元格数据
/// </summary>
/// <param name="sheet">目标单元格所在sheet页</param>
/// <param name="nRowIndex">目标单元格行下标</param>
/// <param name="nCellIndex">目标单元格列下标</param>
/// <param name="sCellVaule">目标单元格值</param>
/// <param name="cellStyle">目标单元格样式</param>
/// <returns>是否设置成功(0:成功,其他:不成功)</returns>
private int SetCellValue(ISheet sheet, int nRowIndex, int nCellIndex, string sCellVaule, ICellStyle cellStyle)
{
//相关参数合法性检查
if (null == sheet || 0 > nRowIndex || 0 > nCellIndex || "" == sCellVaule)
{
return -1;
}
//得到目标单元格所在行,从而确定是是否需要新建行
IRow row = sheet.GetRow(nRowIndex);
ICell cell = null;
if (null == row)
{
row = sheet.CreateRow(nRowIndex);
cell = row.CreateCell(nCellIndex);
}
else
{
//得到目标单元格是否存在,从而确定是否需要新建单元格
if (null == row.GetCell(nCellIndex))
{
cell = row.CreateCell(nCellIndex);
}
else
{
cell = row.GetCell(nCellIndex);
}
}
//设置单元格样式
cell.CellStyle = cellStyle;
//设置单元格的值
cell.SetCellValue(sCellVaule);
//if (-1 != sCellVaule.IndexOf('\n'))//手动计算行,从未设置行高
//{
// //string[] Lines = sCellVaule.Split('\n');
// //if (Lines.Length > sheet.DefaultRowHeight)
// //{
// // row.Height = (short)(sheet.DefaultRowHeight * Lines.Length);
// //}
//}
return 0;
}
代码如上,说明都在注释