您的位置:首页 > 其它

Excel模板导出(针对复杂报表的一种解决方式)

2011-12-14 09:58 791 查看
比如需导出如下形式的报表:



里面数据字段分类很多,又含公式统计等情况。

解决方案:利用NPOI组件,制作导出模板,对单元格精确控制,通过读取单元格里的模板字段,进行数据匹配替换;模板形式如下:



核心方法

/// <summary>
/// 根据Excel模板单元格内容,找出单元格,并设置单元格的值
/// </summary>
/// <param name="sheet">ExcelSheet</param>
/// <param name="rowIndex">行索引</param>
/// <param name="cellTemplateValue">模板内容</param>
/// <param name="cellFillValue">单元格值</param>
///  <param name="conNextRow">是否承接下一行,即:填充下一行单元格模板内容</param>
public static void SetCellValueByTemplateStr(HSSFSheet sheet, int rowIndex, string cellTemplateValue, object cellFillValue, bool conNextRow = true)
{
int cellStartIndex = sheet.GetRow(rowIndex).FirstCellNum;
int cellEndIndex = sheet.GetRow(rowIndex).LastCellNum;
bool find = false;
for (int i = cellStartIndex; i < cellEndIndex; i++)
{
if (find)
break;
else
if (i < (cellEndIndex - cellStartIndex) / 2)
{
#region 折半查找:前半段
for (int j = i; j < (cellEndIndex - cellStartIndex) / 2; j++)
{
if (find)
break;
else
{
HSSFCell cell = sheet.GetRow(rowIndex).GetCell(j);
if (cell != null)
{
if (cell.CellType == 1)
{
string strCellValue = sheet.GetRow(rowIndex).GetCell(j).StringCellValue;
if (string.Compare(strCellValue, cellTemplateValue, true) == 0)
{
find = true;
Type type = cellFillValue.GetType();
switch (type.ToString())
{
case "System.String":
string strValue = cellFillValue.ToString();
sheet.GetRow(rowIndex).GetCell(j).SetCellValue(strValue);
break;
case "System.Int32":
int intValue = Convert.ToInt32(cellFillValue.ToString());
sheet.GetRow(rowIndex).GetCell(j).SetCellValue(intValue);
break;
case "System.Decimal":
double decimalValue = Convert.ToDouble(cellFillValue.ToString());
sheet.GetRow(rowIndex).GetCell(j).SetCellValue(decimalValue);
break;
}
if (conNextRow)//如果承接下一行,则设置下一行单元格里的模板内容
{
sheet.GetRow(rowIndex + 1).GetCell(j).SetCellValue(cellTemplateValue);
sheet.GetRow(rowIndex + 1).GetCell(j).SetCellType(1);
}
}
}
}
}
}
#endregion
}
else
{
#region 折半查找:后半段
for (int j = (cellEndIndex - cellStartIndex) / 2; j < cellEndIndex; j++)
{
if (find)
break;
else
{
HSSFCell cell = sheet.GetRow(rowIndex).GetCell(j);
if (cell != null)
{
if (cell.CellType == 1)
{
string strCellValue = sheet.GetRow(rowIndex).GetCell(j).StringCellValue;
if (string.Compare(strCellValue, cellTemplateValue, true) == 0)
{
find = true;
Type type = cellFillValue.GetType();
switch (type.ToString())
{
case "System.String":
string strValue = cellFillValue.ToString();
sheet.GetRow(rowIndex).GetCell(j).SetCellValue(strValue);
break;
case "System.Int32":
int intValue = Convert.ToInt32(cellFillValue.ToString());
sheet.GetRow(rowIndex).GetCell(j).SetCellValue(intValue);
break;
case "System.Decimal":
double decimalValue = Convert.ToDouble(cellFillValue.ToString());
sheet.GetRow(rowIndex).GetCell(j).SetCellValue(decimalValue);
break;
}
if (conNextRow)//如果承接下一行,则设置下一行单元格里的模板内容
{
sheet.GetRow(rowIndex + 1).GetCell(j).SetCellValue(cellTemplateValue);
sheet.GetRow(rowIndex + 1).GetCell(j).SetCellType(1);
}
}
}
}
}
}
#endregion
}
}
}
Demo 下载
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: