java - POI 单元格不会变色

标签 java excel apache-poi

我试图制作一个读取图像的程序。在 MS Excel 中绘制图像后,每个单元格都是单个像素,如 here .我想我很安静,但我看不到问题,它不会使细胞着色,有人可以帮助我吗?

这是代码,如果您愿意,可以随意使用。

    public class Engine {


    ArrayList<Color> arr = new ArrayList<Color>();
    private int xx;
    private int yy;
    FileOutputStream out;
    HSSFSheet sheet;
    HSSFWorkbook wb;


    public void process() throws AWTException, IOException{

        wb = new HSSFWorkbook();
        sheet = wb.createSheet();
        wb.setActiveSheet(0);
        BufferedImage img = null;
        try {
            img = ImageIO.read(new File("res/images.jpg"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("img file not found");
        }


                  for(int x=0;x<img.getWidth();x++){
                      xx++;
                  for(int y=0;y<img.getHeight();y++){
                      yy++;
                    int rgb = img.getRGB(x, y);
                    Color c = new Color(rgb);
                    printPixelARGB(rgb);
                    arr.add(c);

                    System.out.println("x: "+ x + " y:" + y +" color: " + c);


                 }}
                  out = new FileOutputStream("pic.xls");
                  wb.write(out);
                  out.close();
               }

             public void printPixelARGB(int pixel)  {
                int alpha = (pixel >> 24) & 0xff;
                int red = (pixel >> 16) & 0xff;
                int green = (pixel >> 8) & 0xff;
                int blue = (pixel) & 0xff;

                    HSSFPalette palette = wb.getCustomPalette();
                        HSSFCellStyle style = wb.createCellStyle();
                        HSSFRow row = sheet.createRow((short) yy);
                        HSSFCell cell = row.createCell((short) xx);
                        cell.setCellValue(yy);
                        style.setFillForegroundColor(HSSFColor.LIME.index);
                        style.setFillBackgroundColor(HSSFColor.LIME.index);
                        palette.setColorAtIndex(HSSFColor.LIME.index, (byte) red, (byte) green, (byte) blue);
                        cell.setCellStyle(style);

              }

}

最佳答案

我可以自己解决

解决办法是:

public class Engine {


    ArrayList<Color> arr = new ArrayList<Color>();
    private int xx;
    private int yy;
    FileOutputStream out;
    HSSFSheet sheet;
    HSSFWorkbook wb;
    Row r;

    public void process() throws AWTException, IOException{

        wb = new HSSFWorkbook();
        sheet = wb.createSheet();
        wb.setActiveSheet(0);
        BufferedImage img = null;
        try {
            img = ImageIO.read(new File("res/images.jpg"));
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("img file not found");
        }


                  for(int x=0;x<img.getWidth();x++){
                     xx++;
                     yy=0;
                      r = sheet.createRow(xx);
                  for(int y=0;y<img.getHeight();y++){
                     yy++;
                    int rgb = img.getRGB(x, y);
                    Color c = new Color(rgb);
                    printPixelARGB(rgb);
                    arr.add(c);

                    System.out.println("x: "+ x + " y:" + y +" color: " + c);


                 }}
                  out = new FileOutputStream("pic.xls");
                  wb.write(out);
                  out.close();
               }

             public void printPixelARGB(int pixel)  {
                int alpha = (pixel >> 24) & 0xff;
                int red = (pixel >> 16) & 0xff;
                int green = (pixel >> 8) & 0xff;
                int blue = (pixel) & 0xff;

                    Cell c = r.createCell(yy);
                    HSSFCellStyle style = wb.createCellStyle();
                    HSSFColor col = setColor(wb, (byte)red, (byte)green,(byte)blue);
                    style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
                    style.setFillForegroundColor(col.getIndex());
                    c.setCellStyle(style);

              }

             public HSSFColor setColor(HSSFWorkbook workbook, byte r,byte g, byte b){
                 HSSFPalette palette = workbook.getCustomPalette();
                 HSSFColor hssfColor = null;
                 try {
                 hssfColor= palette.findSimilarColor(r, g, b); 
                 if (hssfColor == null ){
                     palette.setColorAtIndex(HSSFColor.LAVENDER.index, r, g,b);
                     hssfColor = palette.getColor(HSSFColor.LAVENDER.index);
                 }
                  } catch (Exception e) {
                 System.out.println("error");
                 }

                  return hssfColor;
                 }

}

关于java - POI 单元格不会变色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16163741/

相关文章:

java - 嵌入式 jetty 是否支持 java 8 紧凑配置文件?

java - 如何在 Java 中为现有的 REST 服务实现 SSL?

Excel宏来比较行

excel - VBA:将两列或更多列中的值复制到具有相应行的一列中

Java:未找到 Openxmlformats.schemas.officeDocument.x2006.docPropsVTypes.CTArray

java - "System library (added to the boot class path)"在Eclipse中意味着什么?

java - ?在打印心形符号时

excel - 返回下一个包含值范围的所有单元格

java - 使用 Java Apache POI 在 Excel 中插入一行

java - 使用 Apache POI 双向处理 Word 文档