通用Excel模板下载(依据表单明细列自动生成,包含下拉值)

master
xiaochanghai 5 months ago
parent ad4286710d
commit a28259bace
  1. 207
      Tiobon.Core.DataAccess/ReportHelper.cs

@ -166,137 +166,124 @@ public static class ReportHelper
if (rowIndex == 655350 || rowIndex == 0)
{
if (rowIndex != 0)
{
sheet = workbook.CreateSheet();
}
intTop = 0;
#region 列头及样式
IRow headerRow = sheet.CreateRow(intTop);
headerRow.HeightInPoints = HeightInPoints;
intTop += 1;
ICellStyle headStyle = workbook.CreateCellStyle();
headStyle.Alignment = HorizontalAlignment.Center; //居中
headStyle.VerticalAlignment = VerticalAlignment.Center;//垂直居中
headStyle.WrapText = true;//自动换行
// 边框
headStyle.BorderBottom = BorderStyle.Thin;
headStyle.BorderLeft = BorderStyle.Thin;
headStyle.BorderRight = BorderStyle.Thin;
headStyle.BorderTop = BorderStyle.Thin;
ICellStyle headRequiredStyle = workbook.CreateCellStyle();
headRequiredStyle.Alignment = HorizontalAlignment.Center; //居中
headRequiredStyle.VerticalAlignment = VerticalAlignment.Center;//垂直居中
headRequiredStyle.WrapText = true;//自动换行
// 边框
headRequiredStyle.BorderBottom = BorderStyle.Thin;
headRequiredStyle.BorderLeft = BorderStyle.Thin;
headRequiredStyle.BorderRight = BorderStyle.Thin;
headRequiredStyle.BorderTop = BorderStyle.Thin;
// 字体
IFont font = workbook.CreateFont();
font.FontHeightInPoints = (short)10;
font.IsBold = true;
font.FontName = "宋体";
headStyle.SetFont(font);
IFont font2 = workbook.CreateFont();
font2.FontHeightInPoints = (short)10;
font2.IsBold = true;
font2.FontName = "宋体";
font2.Color = IndexedColors.Red.Index;
headRequiredStyle.SetFont(font2);
#endregion
for (int j = 0; j < tableColumn.Count; j++)
{
IRow headerRow = sheet.CreateRow(intTop);
headerRow.HeightInPoints = HeightInPoints;
intTop += 1;
ICellStyle headStyle = workbook.CreateCellStyle();
headStyle.Alignment = HorizontalAlignment.Center; //居中
headStyle.VerticalAlignment = VerticalAlignment.Center;//垂直居中
headStyle.WrapText = true;//自动换行
// 边框
headStyle.BorderBottom = BorderStyle.Thin;
headStyle.BorderLeft = BorderStyle.Thin;
headStyle.BorderRight = BorderStyle.Thin;
headStyle.BorderTop = BorderStyle.Thin;
ICellStyle headRequiredStyle = workbook.CreateCellStyle();
headRequiredStyle.Alignment = HorizontalAlignment.Center; //居中
headRequiredStyle.VerticalAlignment = VerticalAlignment.Center;//垂直居中
headRequiredStyle.WrapText = true;//自动换行
// 边框
headRequiredStyle.BorderBottom = BorderStyle.Thin;
headRequiredStyle.BorderLeft = BorderStyle.Thin;
headRequiredStyle.BorderRight = BorderStyle.Thin;
headRequiredStyle.BorderTop = BorderStyle.Thin;
// 字体
IFont font = workbook.CreateFont();
font.FontHeightInPoints = (short)10;
font.IsBold = true;
font.FontName = "宋体";
headStyle.SetFont(font);
IFont font2 = workbook.CreateFont();
font2.FontHeightInPoints = (short)10;
font2.IsBold = true;
font2.FontName = "宋体";
font2.Color = IndexedColors.Red.Index;
headRequiredStyle.SetFont(font2);
//ICellStyle headStyle = workbook.CreateCellStyle();
//headStyle.Alignment = HorizontalAlignment.Center;
//headStyle.BorderBottom = BorderStyle.Medium;
//headStyle.FillBackgroundColor = NPOI.HSSF.Util.HSSFColor.LightGreen.Index;
//headStyle.FillPattern = FillPattern.NoFill;
//IFont font = workbook.CreateFont();
//font.Boldweight = 700;
//headStyle.SetFont(font);
for (int j = 0; j < tableColumn.Count; j++)
string dataSource = tableColumn[j].dataSource;
string field = tableColumn[j].field;
string label = tableColumn[j].label;
headerRow.CreateCell(j).SetCellValue(label);
if (tableColumn[j].required == "true")
headerRow.GetCell(j).CellStyle = headRequiredStyle;
else
headerRow.GetCell(j).CellStyle = headStyle;
//设置列宽
//sheet.SetColumnWidth(column.Ordinal, (arrColWidth[column.Ordinal] + 1) * 256);
if (arrColWidth[j] > 255)
{
string dataSource = tableColumn[j].dataSource;
string field = tableColumn[j].field;
string label = tableColumn[j].label;
headerRow.CreateCell(j).SetCellValue(label);
if (tableColumn[j].required == "true")
headerRow.GetCell(j).CellStyle = headRequiredStyle;
else
headerRow.GetCell(j).CellStyle = headStyle;
//设置列宽
//sheet.SetColumnWidth(column.Ordinal, (arrColWidth[column.Ordinal] + 1) * 256);
if (arrColWidth[j] > 255)
{
arrColWidth[j] = 254;
}
else
{
sheet.SetColumnWidth(j, (arrColWidth[j] + 1) * 256);
}
arrColWidth[j] = 254;
}
else
{
sheet.SetColumnWidth(j, (arrColWidth[j] + 1) * 256);
}
//是否下拉
if (!string.IsNullOrEmpty(dataSource)
&& !dataSource.StartsWith("OrgTreeWith")
&& !dataSource.StartsWith("StaffWith")
)
{
var dataSourceLists = GetDataSourceLists(Db, field, dataSource);
//是否下拉
if (!string.IsNullOrEmpty(dataSource)
&& !dataSource.StartsWith("OrgTreeWith")
&& !dataSource.StartsWith("StaffWith")
)
{
var dataSourceLists = GetDataSourceLists(Db, field, dataSource);
//var fields = new List<string>()
//{
// "InOrOut",
// "ResumeType",
// "ImportanceDegree",
// "YearHumanId",
// "TitleId"
//};
try
{
if (dataSourceLists.Any())
{
//设置数据源的值
IDataValidationConstraint dvConstraint1 = sheet.GetDataValidationHelper().CreateExplicitListConstraint(dataSourceLists.Where(x => x.name.IsNotEmptyOrNull()).Select(x => x.name).ToArray());
//设置编辑的区域
IDataValidation dataValidation1 = sheet.GetDataValidationHelper().CreateValidation(dvConstraint1, new CellRangeAddressList(1, 65535, j, j));
sheet.AddValidationData(dataValidation1);
}
}
catch (Exception E)
//var fields = new List<string>()
//{
// "InOrOut",
// "ResumeType",
// "ImportanceDegree",
// "YearHumanId",
// "TitleId"
//};
try
{
if (dataSourceLists.Any())
{
//设置数据源的值
IDataValidationConstraint dvConstraint1 = sheet.GetDataValidationHelper().CreateExplicitListConstraint(dataSourceLists.Where(x => x.name.IsNotEmptyOrNull()).Select(x => x.name).ToArray());
//设置编辑的区域
IDataValidation dataValidation1 = sheet.GetDataValidationHelper().CreateValidation(dvConstraint1, new CellRangeAddressList(1, 65535, j, j));
sheet.AddValidationData(dataValidation1);
}
// 多选 ,自行输入不报错
//if (drow.elementType.ToString().LastIndexOf("multiple") == -1)
//{
// dataValidate.CreateErrorBox("输入不合法", "请输入或选择下拉列表中的值。");
// dataValidate.ShowPromptBox = true;
//}
//else
//{
// dataValidate.ShowErrorBox = false;
//}
}
catch (Exception E)
{
}
// 多选 ,自行输入不报错
//if (drow.elementType.ToString().LastIndexOf("multiple") == -1)
//{
// dataValidate.CreateErrorBox("输入不合法", "请输入或选择下拉列表中的值。");
// dataValidate.ShowPromptBox = true;
//}
//else
//{
// dataValidate.ShowErrorBox = false;
//}
}
}
#endregion
rowIndex = intTop;
}
#endregion
#region 填充内容
IRow dataRow = sheet.CreateRow(rowIndex);
dataRow.HeightInPoints = 25;
//IRow dataRow = sheet.CreateRow(rowIndex);
//dataRow.HeightInPoints = 25;
ICellStyle style = workbook.CreateCellStyle();
style.Alignment = HorizontalAlignment.Center; //居中

Loading…
Cancel
Save