NPOI边框问题,导出的excel边框不全,请教..

wzc7332178 2014-12-30 10:34:21
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);
}

}



怎样才能全部显示边框,求指教,谢谢!
...全文
1706 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wzc7332178 2015-01-13
  • 打赏
  • 举报
回复
问题暂时解决了,因为我添加边框是一格一格添加的,后来处理为一列一列添加边框。
       
                //构建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++;
                        }
wzc7332178 2014-12-30
  • 打赏
  • 举报
回复
引用 3 楼 Z65443344 的回复:
dateStyle是干什么的
那个发现多余了,谢谢!
wzc7332178 2014-12-30
  • 打赏
  • 举报
回复
引用 5 楼 lc2737 的回复:
[quote=引用 4 楼 lc2737 的回复:] 你的rowIndex永远都是1?
sorry,没注意看,仔细看了一下你的代码,发现代码部分貌似都没有问题。 你加个Try Catch吧,要不然调试中断什么的。。。[/quote] 嗯,谢谢,正在调试..
ajaxfeifei 2014-12-30
  • 打赏
  • 举报
回复
引用 4 楼 lc2737 的回复:
你的rowIndex永远都是1?
sorry,没注意看,仔细看了一下你的代码,发现代码部分貌似都没有问题。 你加个Try Catch吧,要不然调试中断什么的。。。
ajaxfeifei 2014-12-30
  • 打赏
  • 举报
回复
你的rowIndex永远都是1?
於黾 2014-12-30
  • 打赏
  • 举报
回复
dateStyle是干什么的
wzc7332178 2014-12-30
  • 打赏
  • 举报
回复
引用 1 楼 yuyang00000 的回复:
调试下看看呗,是不是每一行
调试了,中间断了..why..不清楚原因
渃水 2014-12-30
  • 打赏
  • 举报
回复
调试下看看呗,是不是每一行

4,816

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 图表区
社区管理员
  • 图表区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧