当前位置:编程学习 > C#/ASP.NET >>

设置excel单元格式为什么没有效果

  //创建工作薄
                HSSFWorkbook workBook = new HSSFWorkbook();
                //创建一个名称为Sheet1的表
                HSSFSheet sheet = workBook.CreateSheet("Sheet1");
                //------------------第一行  标题--------------------
                HSSFRow titleRow = sheet.CreateRow(0);//创建标题行
                HSSFCell titleCell = titleRow.CreateCell(0);
                titleCell.SetCellValue(name + "抽签结果");
                HSSFCellStyle titleStyle = workBook.CreateCellStyle();
                titleStyle.Alignment = HSSFCellStyle.ALIGN_CENTER;
                HSSFFont font = workBook.CreateFont();
                font.FontHeightInPoints = 20;
                titleStyle.SetFont(font);
                titleCell.CellStyle = titleStyle;
                //创建合并区域 参数(开始行,结束行,开始列,结束列)
                CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, 5);
                sheet.AddMergedRegion(cellRangeAddress);
                //----------------第二行 表头--------------------------
                HSSFRow row = sheet.CreateRow(1);
                //创建第1列
                HSSFCell cell1 = row.CreateCell(0);
                cell1.SetCellValue("序号");
                sheet.SetColumnWidth(0, 5 * 256);
                //创建第2列
                HSSFCell cell2 = row.CreateCell(1);
                //设置单元格的宽度  1为列 
                sheet.SetColumnWidth(1, 16 * 256);
                cell2.SetCellValue("选手编号");
                //创建第3列
                HSSFCell cell3 = row.CreateCell(2);
                cell3.SetCellValue("姓名");
                //sheet.SetColumnWidth(2, 7 * 256);
                HSSFCell cell4 = row.CreateCell(3);
                sheet.SetColumnWidth(3, 20 * 256);
                cell4.SetCellValue("身份证号码");
                HSSFCell cell5 = row.CreateCell(4);
                sheet.SetColumnWidth(4, 28 * 256);
                cell5.SetCellValue("代表团名称");
                HSSFCell cell6 = row.CreateCell(5);
                cell6.SetCellValue("签名");
                //-------------------写入数据-----------------------------
                for (int i = 0; i < stus.Length; i++)
                {
                    Student stu = stus[i];
                    //创建第二行,开始写数据
                    row = sheet.CreateRow(i + 2);
                    //创建第1列
                    cell1 = row.CreateCell(0);
                    string strId="";
                    if(stu.Id ==0)
                    {
                        strId = stu.PlayerNumber;

                        HSSFCellStyle tStyle = workBook.CreateCellStyle();
                        tStyle.Alignment = HSSFCellStyle.ALIGN_CENTER;
                        HSSFFont font1 = workBook.CreateFont();
                        font1.FontHeightInPoints = 16;
                        tStyle.SetFont(font1);
                        cell1.CellStyle = tStyle;

                        cellRangeAddress = new CellRangeAddress(i + 2, i + 2, 0, 5);
                        sheet.AddMergedRegion(cellRangeAddress);
                    }
                    else
                    {
                        strId = stu.Id.ToString();
                    }


                    cell1.SetCellValue(strId);
                    //创建第2列
                    cell2 = row.CreateCell(1);
                    cell2.SetCellValue(stu.PlayerNumber);
                    //创建第3列
                    cell3 = row.CreateCell(2);
                    cell3.SetCellValue(stu.Name);
                    cell4 = row.CreateCell(3);
                    cell4.SetCellValue(stu.IDCard1);
                    cell5 = row.CreateCell(4);
                    cell5.SetCellValue(stu.School);
                    cell6 = row.CreateCell(5);
                    cell6.SetCellValue("  ");

                 
                   
                }
                //-------------------设置单元格边框-------------------------
                HSSFCellStyle style = workBook.CreateCellStyle();
                for (int i = 1; i <= sheet.LastRowNum; i++)
                {
                    row = sheet.GetRow(i);
                    for (int j = 0; j <row.LastCellNum; j++)
                    {
                        HSSFCell cell = row.GetCell(j);
                        style.BorderBottom = HSSFCellStyle.BORDER_THIN;
                        style.BorderLeft = HSSFCellStyle.BORDER_THIN;
                        style.BorderRight = HSSFCellStyle.BORDER_THIN;
                        style.BorderTop = HSSFCellStyle.BORDER_THIN;
                        style.WrapText = true;
                        cell.CellStyle = style;
                    }
                }
                //打开一个xls文件,如果没有则自行创建,如果存在myxls.xls文件则在创建是不要打开该文件!
                using (FileStream fs = File.OpenWrite(sfd.FileName))
                {
                    //向打开的这个xls文件中写入mySheet表并保存。
                    workBook.Write(fs);  
                    MessageBox.Show("提示:创建成功!");
                }

就是红色那段没效果,上面的标题还是有效果
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,