Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Apache POI设置单元格边框不工作_Java_Apache Poi - Fatal编程技术网

Java Apache POI设置单元格边框不工作

Java Apache POI设置单元格边框不工作,java,apache-poi,Java,Apache Poi,我在ApachePOI(V4.1.2)中遇到了一个问题,它无法将边界设置为薄边界。 我的代码同时支持XLS和XLSX扩展。当我将数据导出到XLSX文件时,一切正常,但对于XLS文件,它不会从单元格K5到末尾绘制边框 这是我的密码: private int populateExcelData(Workbook workbook, Sheet sheet, int rowNum, List<List<Object>> excelData, Excel

我在ApachePOI(V4.1.2)中遇到了一个问题,它无法将边界设置为薄边界。 我的代码同时支持XLS和XLSX扩展。当我将数据导出到XLSX文件时,一切正常,但对于XLS文件,它不会从单元格K5到末尾绘制边框

这是我的密码:

private int populateExcelData(Workbook workbook, Sheet sheet, int rowNum, List<List<Object>> excelData,
            ExcelVo excelVo, Boolean isHeader) {
        if (CollectionUtils.isNotEmpty(excelData)) {
            int cellNum = 0;
            Row row;
            for (List<Object> objects : excelData) {
                cellNum = excelVo.getColPadding();
                row = sheet.createRow(rowNum++);
                Cell cell;
                for (Object object : objects) {
                    cell = row.createCell(cellNum++);
                    setCellValue(cell, object, excelVo);
                    configCellStyle(cell, workbook, excelVo, isHeader);
                }
            }
        }
        return rowNum;
    }

private void configCellStyle(Cell cell, Workbook workbook, ExcelVo excelVo, Boolean isHeader) {
        CellStyle cellStyle = workbook.createCellStyle();
        cellStyle.setBorderBottom(BorderStyle.THIN);
        cellStyle.setBorderLeft(BorderStyle.THIN);
        cellStyle.setBorderRight(BorderStyle.THIN);
        cellStyle.setBorderTop(BorderStyle.THIN);
        if (isHeader) {
            Font headerFont = workbook.createFont();
            headerFont.setBold(true);
            headerFont.setColor(excelVo.getFontColor().getIndex());
            cellStyle.setFont(headerFont);
            cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            cellStyle.setFillForegroundColor(excelVo.getBackgroundColor().getIndex());
        }
        cell.setCellStyle(cellStyle);
    }
private int populateExcelData(工作簿、工作表、int rowNum、列表数据、,
ExcelVo ExcelVo,布尔isHeader){
if(CollectionUtils.isNotEmpty(excelData)){
int-cellNum=0;
行行;
用于(列出对象:excelData){
cellNum=excelVo.getColPadding();
row=sheet.createRow(rowNum++);
细胞;
用于(对象:对象){
cell=row.createCell(cellNum++);
setCellValue(单元格、对象、excelVo);
configCellStyle(单元格、工作簿、excelVo、isHeader);
}
}
}
返回rowNum;
}
专用void configCellStyle(单元格、工作簿工作簿、ExcelVo ExcelVo、布尔isHeader){
CellStyle CellStyle=workbook.createCellStyle();
cellStyle.Bottom(BorderStyle.THIN);
cellStyle.Left(BorderStyle.THIN);
cellStyle.Right(BorderStyle.THIN);
cellStyle.Top(BorderStyle.THIN);
如果(isHeader){
Font-headerFont=workbook.createFont();
headerFont.setBold(真);
setColor(excelVo.getFontColor().getIndex());
cellStyle.setFont(headerFont);
cellStyle.setFillPattern(FillPatternType.SOLID\u前景);
setFillForegroundColor(excelVo.getBackgroundColor().getIndex());
}
cell.setCellStyle(cellStyle);
}
我不知道为什么XLS文件只从单元格K5开始绘制边框。代码不会抛出错误或异常

谢谢大家的支持。

有。单元格样式和字体存储在工作簿级别。所有工作表中的所有单元格都将共享它们。当前Excel版本限制为:

独特的单元格格式/单元格样式:65490

独特的字体类型:可使用1024种全局字体;512% 工作手册

以前版本的Excel(二进制*.xls)甚至有更小的限制

因此,如果您正在为每个单元创建单独的单元样式,您将很快达到极限<代码>XSSF可能会工作,因为限制更大。但以前版本的Excel(二进制*.xls)甚至有更小的限制。这就是为什么它不使用
HSSF

因此,要做的是,在工作簿级别上创建工作簿所需的尽可能多的单元格样式。在单元格填充过程之外执行此操作。在创建单元格本身时,只能使用
cell.setCellStyle
应用单元格样式。但它无法在那时创建

让我们用一个完整的例子来说明这一点。它部分使用了您的
populateExcelData
代码,但我只希望它符合您的想法,因为您没有提供完整的示例

import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import java.util.GregorianCalendar;
import java.util.List;
import java.util.ArrayList;

import org.apache.commons.collections4.CollectionUtils;

public class CreateExcel {

 private Workbook workbook;
 private CellStyle textStyle;
 private CellStyle dateStyle;
 private CellStyle numberStyle;
 private CellStyle headerStyle;

 public CreateExcel(String type, String path, List<List<Object>> headerData, List<List<Object>> excelData) {
  try {
   this.workbook = ("HSSF".equals(type)) ? new HSSFWorkbook() : new XSSFWorkbook(); 

   DataFormat dataFormat = workbook.createDataFormat();

   this.textStyle = workbook.createCellStyle();
   setCellStyleAllBorders(textStyle);

   this.dateStyle = workbook.createCellStyle();
   dateStyle.setDataFormat(dataFormat.getFormat("DDDD, MMMM, DD, YYYY"));
   setCellStyleAllBorders(dateStyle);

   this.numberStyle = workbook.createCellStyle();
   numberStyle.setDataFormat(dataFormat.getFormat("#,##0.00 \" Coins\""));
   setCellStyleAllBorders(numberStyle);

   this.headerStyle = workbook.createCellStyle();
   Font headerFont = workbook.createFont();
   headerFont.setBold(true);
   headerFont.setColor(IndexedColors.WHITE.getIndex());
   headerStyle.setFont(headerFont);
   headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
   headerStyle.setFillForegroundColor(IndexedColors.BLACK.getIndex());
   setCellStyleAllBorders(headerStyle);

   Sheet sheet = workbook.createSheet(); 

   int rowNum = 0;
   rowNum = populateExcelData(workbook, sheet, rowNum, headerData, 1, true);

   rowNum = populateExcelData(workbook, sheet, rowNum, excelData, 1, false);
   
   System.out.println(rowNum);

   for (int c = 0; c < 256; c++) {
    sheet.autoSizeColumn(c); // this is very time consuming, would be better one can set column widths using Sheet.setColumnWidth(int columnIndex, int width) directly
   }

   FileOutputStream fileout = new FileOutputStream(path);
   this.workbook.write(fileout);
   fileout.close();
   this.workbook.close();

  } catch (Exception ex) {
   ex.printStackTrace(); 
  }
 }

 private int populateExcelData(Workbook workbook, Sheet sheet, int rowNum, List<List<Object>> excelData, Integer colPadding, Boolean isHeader) {
  if (CollectionUtils.isNotEmpty(excelData)) {
   int cellNum = 0;
   Row row;
   for (List<Object> objects : excelData) {
    cellNum = colPadding;
    row = sheet.createRow(rowNum++);
    Cell cell;
    for (Object object : objects) {
     cell = row.createCell(cellNum++);
     setCellValueAndStyle(cell, object, isHeader);
    }
   }
  }
  return rowNum;
 }

 private void setCellValueAndStyle(Cell cell, Object object, Boolean isHeader) {
  if (object instanceof String) {
   cell.setCellValue((String) object);
   if (!isHeader) cell.setCellStyle(this.textStyle);
  } else if (object instanceof Double) {
   cell.setCellValue((Double) object);
   if (!isHeader) cell.setCellStyle(this.numberStyle);
  } else if (object instanceof GregorianCalendar) {
   cell.setCellValue((GregorianCalendar) object);
   if (!isHeader) cell.setCellStyle(this.dateStyle);
  }
  if (isHeader) cell.setCellStyle(this.headerStyle);
 }

 private void setCellStyleAllBorders(CellStyle cellStyle) {
  cellStyle.setBorderBottom(BorderStyle.THIN);
  cellStyle.setBorderLeft(BorderStyle.THIN);
  cellStyle.setBorderRight(BorderStyle.THIN);
  cellStyle.setBorderTop(BorderStyle.THIN);
 }

 public static void main(String[] args) throws Exception {
  List<List<Object>> headerData = new ArrayList<>();
  List<Object> headerRow = new ArrayList<>();
  headerRow.add("Text"); headerRow.add("Value"); headerRow.add("Date");
  headerData.add(headerRow);
  headerRow = new ArrayList<>();
  headerRow.add("not formatted"); headerRow.add("in Coins"); headerRow.add("as long date");
  headerData.add(headerRow);

  List<List<Object>> excelData = new ArrayList<>();
  for (int r = 1; r < 1000; r++) {
   List<Object> excelRow = new ArrayList<>();
   excelRow.add("Text" + r); excelRow.add(123.45 * r); excelRow.add(new GregorianCalendar(2020, 0, r));
   excelData.add(excelRow);
  }

  CreateExcel test = new CreateExcel("HSSF", "./Excel.xls", headerData, excelData);
  test = new CreateExcel("XSSF", "./Excel.xlsx", headerData, excelData);
 }
}
import java.io.FileOutputStream;
导入org.apache.poi.ss.usermodel.*;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
导入org.apache.poi.hssf.usermodel.HSSFWorkbook;
导入java.util.GregorianCalendar;
导入java.util.List;
导入java.util.ArrayList;
导入org.apache.commons.collections4.CollectionUtils;
公共类CreateExcel{
私人工作手册;
私人手机风格文本风格;
私人牢房风格;
私人牢房风格号码风格;
私人牢房式领导风格;
公共CreateExcel(字符串类型、字符串路径、列表标题数据、列表Excel数据){
试一试{
this.workbook=(“HSSF.equals(type))?新的HSSFWorkbook():新的XSSFWorkbook();
DataFormat DataFormat=工作簿.createDataFormat();
this.textStyle=workbook.createCellStyle();
setCellStyleAllBorders(textStyle);
this.dateStyle=workbook.createCellStyle();
setDataFormat(dataFormat.getFormat(“DDDD,MMMM,DD,YYYY”);
setCellStyleAllBorders(日期样式);
this.numberStyle=workbook.createCellStyle();
numberStyle.setDataFormat(dataFormat.getFormat(“#,#,##0.00\'Coins\”);
setCellStyleAllBorders(数字样式);
this.headerStyle=workbook.createCellStyle();
Font-headerFont=workbook.createFont();
headerFont.setBold(真);
setColor(IndexedColors.WHITE.getIndex());
headerStyle.setFont(headerFont);
headerStyle.setFillPattern(FillPatternType.SOLID\u前景);
headerStyle.setFillForegroundColor(IndexedColors.BLACK.getIndex());
setCellStyleAllBorders(headerStyle);
工作表=工作簿.createSheet();
int rowNum=0;
rowNum=populateExcelData(工作簿、工作表、rowNum、headerData,1,true);
rowNum=populateExcelData(工作簿、工作表、rowNum、excelData,1,false);
System.out.println(rowNum);
用于(int c=0;c<256;c++){
sheet.autoSizeColumn(c);//这非常耗时,最好直接使用sheet.setColumnWidth(int columnIndex,int width)设置列宽
}
FileOutputStream fileout=新的FileOutputStream(路径);
这个.工作簿.写入(文件输出);
fileout.close();
this.workbook.close();
}捕获(例外情况除外){
例如printStackTrace();
}
}
私有int populateExcelData(工作簿工作簿、工作表、int rowNum、List excelData、整数列填充、布尔isHeader){
if(CollectionUtils.isNotEmpty(excelData)){
int-cellNum=0;
行行;
用于(列出对象:excelData){
cellNum=colPadding;
row=sheet.createRow(rowNum++);
细胞;
用于(对象:对象){
cell=row.createCell(cellNum++);
setCellValueAndStyle(单元格、对象、isHeader);
}
}
}
返回rowNum;
}
私有void setCellValueAndStyle(单元格、对象对象、布尔isHeader){
if(字符串的对象实例){
cell.setCe