4,816
社区成员
发帖
与我相关
我的任务
分享
using (NPOI.SS.UserModel.Workbook wkbook = new HSSFWorkbook())
{
using (Sheet sheet = wkbook.CreateSheet("本部计划"))
{
HSSFCellStyle dateStyle = (HSSFCellStyle)wkbook.CreateCellStyle();
//取得列宽
int[] arrColWidth = new int[dt.Columns.Count];
foreach (DataColumn item in dt.Columns)
{
arrColWidth[item.Ordinal] = Encoding.GetEncoding(936).GetBytes(item.ColumnName.ToString()).Length;
}
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
int intTemp = Encoding.GetEncoding(936).GetBytes(dt.Rows[i][j].ToString()).Length;
if (intTemp > arrColWidth[j])
{
arrColWidth[j] = intTemp;
}
}
}
//Row heardrow = sheet.CreateRow(0);
//for (int j = 0; j < dt.Columns.Count; j++)
//{
// Cell heardCell = heardrow.CreateCell(j);
// heardCell.SetCellValue(dt.Columns[j].ColumnName);
//}
//for (int i = 0; i < dt.Rows.Count; i++)
//{
// heardrow = sheet.CreateRow(i + 1);
// for (int j = 0; j < dt.Columns.Count; j++)
// {
// Cell heardCell = heardrow.CreateCell(j);
// heardCell.SetCellValue(dt.Rows[i][j].ToString());
// }
//}
int rowIndex = 0;
foreach (DataRow row in dt.Rows)
{
#region 新建表,填充表头,填充列头,样式
if (rowIndex == 65535 || rowIndex == 0)
{
if (rowIndex != 0)
{
wkbook.CreateSheet("本部计划");
}
#region 列头及样式
{
CellStyle style = wkbook.CreateCellStyle();
HSSFRow headerRow = (HSSFRow)sheet.CreateRow(0);//第一行
HSSFCellStyle headStyle = (HSSFCellStyle)wkbook.CreateCellStyle();
headStyle.Alignment = HorizontalAlignment.CENTER;//左右居中
//headStyle.VerticalAlignment = VerticalAlignment.CENTER;//上下居中
//headStyle.WrapText = true;//自动换行
HSSFFont font = (HSSFFont)wkbook.CreateFont();//设置字体
//设置单元格边框
style.BorderBottom = CellBorderType.THIN;//下边框为细线边框
style.BorderLeft = CellBorderType.THIN;//左边框
style.BorderRight = CellBorderType.THIN;//上边框
style.BorderTop = CellBorderType.THIN;//右边框
style.FillForegroundColor = HSSFColor.YELLOW.index;//背景色为黄色
style.FillPattern = FillPatternType.SOLID_FOREGROUND;//填充图案为全色
// heardrow.HeightInPoints = 50f; //- 设置行高
//font.FontName = "新細明體";//字体类型
//font.FontHeightInPoints = 10;//设置字体大小
font.Boldweight = short.MaxValue;//粗体显示
style.SetFont(font);//选择需要用到的字体格式
foreach (DataColumn column in dt.Columns)
{
headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
headerRow.GetCell(column.Ordinal).CellStyle = style;
//设置列宽
sheet.SetColumnWidth(column.Ordinal, (arrColWidth[column.Ordinal] + 1) * 256);//第一个参数代表列id(从0开始),第2个参数代表宽度值 参考 :"2012-08-10"的宽度为2500
}
//headerRow.Dispose();
}
#endregion
rowIndex = 1;
}
#endregion
#region 填充内容
HSSFRow dataRow = sheet.CreateRow(rowIndex) as HSSFRow;
foreach (DataColumn column in dt.Columns)
{
HSSFCell newCell = dataRow.CreateCell(column.Ordinal) as HSSFCell;
string drValue = row[column].ToString();
CellStyle style = wkbook.CreateCellStyle();//样式
style.BorderBottom = CellBorderType.THIN;//下边框为细线边框
style.BorderLeft = CellBorderType.THIN;//左边框
style.BorderRight = CellBorderType.THIN;//上边框
style.BorderTop = CellBorderType.THIN;//右边框
newCell.CellStyle = style;
switch (column.DataType.ToString())
{
case "System.String"://字符串类型
newCell.SetCellValue(drValue);
break;
case "System.DateTime"://日期类型
DateTime dateV;
DateTime.TryParse(drValue, out dateV);
newCell.SetCellValue(dateV);
newCell.CellStyle = dateStyle;//格式化显示
break;
case "System.Boolean"://布尔型
bool boolV = false;
bool.TryParse(drValue, out boolV);
newCell.SetCellValue(boolV);
break;
case "System.Int16"://整型
case "System.Int32":
case "System.Int64":
case "System.Byte":
int intV = 0;
int.TryParse(drValue, out intV);
newCell.SetCellValue(intV);
break;
case "System.Decimal"://浮点型
case "System.Double":
double doubV = 0;
double.TryParse(drValue, out doubV);
newCell.SetCellValue(doubV);
break;
case "System.DBNull"://空值处理
newCell.SetCellValue("");
break;
default:
newCell.SetCellValue("");
break;
}
}
#endregion
rowIndex++;
}
using (FileStream fs = File.OpenWrite(templatePath))
{
wkbook.Write(fs);
}
//sheet.AutoSizeColumn(1);
}
}
//构建DataTable,规则为第一个为任务名,最后一个为层级
DataTable dt = new DataTable();
dt.Columns.Add("生产类型", typeof(string));
dt.Columns.Add("成本对象", typeof(string));
dt.Columns.Add("任务名称", typeof(string));
dt.Columns.Add("任务说明", typeof(string));
dt.Columns.Add("技术说明", typeof(string));
dt.Columns.Add("技术启动时间", typeof(string));
dt.Columns.Add("项目需求时间", typeof(string));
dt.Columns.Add("计划完成数量", typeof(string));
dt.Columns.Add("计量单位", typeof(string));
dt.Columns.Add("执行部门", typeof(string));
dt.Columns.Add("编制依据", typeof(string));
;
foreach (ThispartPlan plan in list)
{
DataRow r = dt.NewRow();
r["生产类型"] = plan.ProductionType;
r["成本对象"] = plan.Costobject;
r["任务名称"] = plan.TaskName;
r["任务说明"] = plan.Taskspecification;
r["技术说明"] = plan.Technicalnote;
r["技术启动时间"] = plan.PlanstartDate;
r["项目需求时间"] = plan.ProjectneedDate;
r["计划完成数量"] = plan.PlanfinishCount;
r["计量单位"] = plan.Unit;
r["执行部门"] = plan.ExDepartments;
r["编制依据"] = plan.CompilationBasis;
dt.Rows.Add(r);
}
using (NPOI.SS.UserModel.Workbook wkbook = new HSSFWorkbook())
{
using (Sheet sheet = wkbook.CreateSheet("本部计划"))
{
HSSFCellStyle dateStyle = (HSSFCellStyle)wkbook.CreateCellStyle();
HSSFDataFormat format = (HSSFDataFormat)wkbook.CreateDataFormat();
dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd");
//取得列宽
int[] arrColWidth = new int[dt.Columns.Count];
foreach (DataColumn item in dt.Columns)
{
arrColWidth[item.Ordinal] = Encoding.GetEncoding(936).GetBytes(item.ColumnName.ToString()).Length;
}
for (int i = 0; i < dt.Rows.Count; i++)
{
for (int j = 0; j < dt.Columns.Count; j++)
{
int intTemp = Encoding.GetEncoding(936).GetBytes(dt.Rows[i][j].ToString()).Length;
if (intTemp > arrColWidth[j])
{
arrColWidth[j] = intTemp;
}
}
}
int rowIndex = 0;
foreach (DataRow row in dt.Rows)
{
#region 新建表,填充表头,填充列头,样式
if (rowIndex == 65535 || rowIndex == 0)
{
#region 列头及样式
{
HSSFCellStyle style = wkbook.CreateCellStyle() as HSSFCellStyle;
HSSFRow headerRow = (HSSFRow)sheet.CreateRow(rowIndex);//第一行
HSSFCellStyle headStyle = (HSSFCellStyle)wkbook.CreateCellStyle();
headStyle.Alignment = HorizontalAlignment.CENTER;//左右居中
//headStyle.VerticalAlignment = VerticalAlignment.CENTER;//上下居中
//headStyle.WrapText = true;//自动换行
HSSFFont font = (HSSFFont)wkbook.CreateFont();//设置字体
//设置单元格边框
style.BorderBottom = CellBorderType.THIN;//下边框为细线边框
style.BorderLeft = CellBorderType.THIN;//左边框
style.BorderRight = CellBorderType.THIN;//上边框
style.BorderTop = CellBorderType.THIN;//右边框
style.FillForegroundColor = HSSFColor.YELLOW.index;//背景色为黄色
style.FillPattern = FillPatternType.SOLID_FOREGROUND;//填充图案为全色
// heardrow.HeightInPoints = 50f; //- 设置行高
//font.FontName = "新細明體";//字体类型
//font.FontHeightInPoints = 10;//设置字体大小
font.Boldweight = short.MaxValue;//粗体显示
style.SetFont(font);//选择需要用到的字体格式
foreach (DataColumn column in dt.Columns)
{
headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
headerRow.GetCell(column.Ordinal).CellStyle = style;
//设置列宽
sheet.SetColumnWidth(column.Ordinal, (arrColWidth[column.Ordinal] + 1) * 256);//第一个参数代表列id(从0开始),第2个参数代表宽度值 参考 :"2012-08-10"的宽度为2500
}
//headerRow.Dispose();
}
#endregion
rowIndex = 1;
}
#endregion
#region 填充内容
//HSSFRow dataRow = (HSSFRow)sheet.CreateRow(rowIndex);
//foreach (DataColumn column in dt.Columns)
//{
// HSSFCellStyle style = wkbook.CreateCellStyle() as HSSFCellStyle;
// style.CloneStyleFrom(style); //克隆前一个样式
// string drValue = row[column].ToString();
// style.BorderBottom = CellBorderType.THIN;//下边框为细线边框
// style.BorderLeft = CellBorderType.THIN;//左边框
// style.BorderRight = CellBorderType.THIN;//上边框
// style.BorderTop = CellBorderType.THIN;//右边框
// // Cell cell = sheet.CreateRow(rowIndex).CreateCell(column.Ordinal);
// //cell.CellStyle = style;
// dataRow.CreateCell(column.Ordinal).SetCellValue(drValue);
// dataRow.GetCell(column.Ordinal).CellStyle = style;
//}
#endregion
}
HSSFCellStyle style1 = wkbook.CreateCellStyle() as HSSFCellStyle;
style1.BorderBottom = CellBorderType.THIN;//下边框为细线边框
style1.BorderLeft = CellBorderType.THIN;//左边框
style1.BorderRight = CellBorderType.THIN;//上边框
style1.BorderTop = CellBorderType.THIN;//右边框
foreach (ThispartPlan work in list)
{
HSSFRow dataRow = (HSSFRow)sheet.CreateRow(rowIndex);
dataRow.CreateCell(0).SetCellValue(work.ProductionType);
dataRow.CreateCell(1).SetCellValue(work.Costobject);
dataRow.CreateCell(2).SetCellValue(work.TaskName);
dataRow.CreateCell(3).SetCellValue(work.Taskspecification);
dataRow.CreateCell(4).SetCellValue(work.Technicalnote);
dataRow.CreateCell(5).SetCellValue(work.PlanstartDate);
dataRow.CreateCell(6).SetCellValue(work.ProjectneedDate);
dataRow.CreateCell(7).SetCellValue(work.PlanfinishCount);
dataRow.CreateCell(8).SetCellValue(work.Unit);
dataRow.CreateCell(9).SetCellValue(work.ExDepartments);
dataRow.CreateCell(10).SetCellValue(work.CompilationBasis);
dataRow.GetCell(0).CellStyle = style1;
dataRow.GetCell(1).CellStyle = style1;
dataRow.GetCell(2).CellStyle = style1;
dataRow.GetCell(3).CellStyle = style1;
dataRow.GetCell(4).CellStyle = style1;
dataRow.GetCell(5).CellStyle = style1;
dataRow.GetCell(6).CellStyle = style1;
dataRow.GetCell(7).CellStyle = style1;
dataRow.GetCell(8).CellStyle = style1;
dataRow.GetCell(9).CellStyle = style1;
dataRow.GetCell(10).CellStyle = style1;
rowIndex++;
}