/// <summary>
/// 绑定数据的方法
/// </summary>
/// <param name="ssfworkbook">HSSFWorkbook</param>
/// <param name="sheet1">HSSFSheet</param>
/// <param name="cateDt">数据源</param>
/// <param name="sheetName">页名</param>
/// <param name="dispName">显示名称</param>
/// <param name="row">属于第几列</param>
/// <param name="nextCount">相隔下一列的位置(只要有一列为1,后面便都为1,以此类推)</param>
private void GetCategoryData(HSSFWorkbook ssfworkbook, HSSFSheet sheet1, DataTable cateDt, string sheetName, string dispName, int row, int nextCount)
{
try
{
HSSFSheet sheet2 = ssfworkbook.CreateSheet("Sht" + sheetName);
int indexCate = ;
ssfworkbook.SetSheetHidden(row - nextCount, true);//隐藏 foreach (DataRow cateItem in cateDt.Rows)
{
sheet2.CreateRow(indexCate).CreateCell().SetCellValue(cateItem[dispName].ToString());//列数据
indexCate++;
}
HSSFName range = ssfworkbook.CreateName();//创建名称
range.Reference = "Sht" + sheetName + "!$A$1:$A$" + indexCate;//格式
range.NameName = sheetName; //将下拉列表添加
CellRangeAddressList regions = null;
regions = new CellRangeAddressList(, , row - , row - );
DVConstraint constraint = DVConstraint.CreateFormulaListConstraint(sheetName);
HSSFDataValidation dataValidate = new HSSFDataValidation(regions, constraint);
sheet1.AddValidationData(dataValidate);
}
catch { }
}
/// <summary>
/// 设置Excel表头(数组Length必须相同)
/// </summary>
/// <param name="strName">Excel中的名称</param>
/// <param name="width">宽</param>
/// <param name="headerRow">HSSFRow</param>
/// <param name="sheet1">HSSFSheet</param>
/// <param name="HSSFWorkbook">ssfworkbook</param>
private string SetUpExcelTitle(HSSFWorkbook ssfworkbook, string[] strName, int[] width, HSSFRow headerRow, HSSFSheet sheet1)
{
try
{
if (strName.Length != width.Length)
{
return "数组长度不同!";
}
HSSFFont fontTitle = ssfworkbook.CreateFont();
fontTitle.FontHeightInPoints = ;
fontTitle.Boldweight = HSSFFont.BOLDWEIGHT_NORMAL;
HSSFCellStyle hssfCellTitle = ssfworkbook.CreateCellStyle();
hssfCellTitle.SetFont(fontTitle); for (int i = ; i < strName.Length; i++)
{
//设置表头
headerRow.CreateCell(i).SetCellValue(strName[i]);
//设置表头的宽度
sheet1.SetColumnWidth(i, width[i]);
headerRow.GetCell(i).CellStyle = hssfCellTitle;
}
return "ok";
}
catch (Exception ex)
{
return ex.Message;
}
}
/// <summary>
/// 设置Excel内容
/// </summary>
/// <param name="values">内容</param>
/// <param name="dataRow">HSSFRow</param>
private void SetUpExcelContent(string[] values, HSSFRow dataRow)
{
for (int i = ; i < values.Length; i++)
{
//填充数据
dataRow.CreateCell(i).SetCellValue(values[i]);
}
}