Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/29.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.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 单元格赢得';不能用POI染色_Java_Excel_Apache Poi - Fatal编程技术网

Java 单元格赢得';不能用POI染色

Java 单元格赢得';不能用POI染色,java,excel,apache-poi,Java,Excel,Apache Poi,我试着制作一个能读入图像的程序。在MS excel中绘制图像后,每个单元格都是单像素的。我想我很安静,但我看不出问题所在,它不会使单元格着色。有人能帮我解决吗 这是你可以自由使用的代码 public class Engine { ArrayList<Color> arr = new ArrayList<Color>(); private int xx; private int yy; FileOutputStream out;

我试着制作一个能读入图像的程序。在MS excel中绘制图像后,每个单元格都是单像素的。我想我很安静,但我看不出问题所在,它不会使单元格着色。有人能帮我解决吗

这是你可以自由使用的代码

    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);

              }

}
公共类引擎{
ArrayList arr=新的ArrayList();
私人int xx;
私人int yy;
文件输出流输出;
HSSF表;
HSSF工作手册wb;
public void process()引发AWTException,IOException{
wb=新的HSSF工作手册();
sheet=wb.createSheet();
wb.setActiveSheet(0);
BuffereImage img=null;
试一试{
img=ImageIO.read(新文件(“res/images.jpg”);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
System.out.println(“未找到img文件”);
}
对于(int x=0;x 24)&0xff;
int red=(像素>>16)和0xff;
绿色整数=(像素>>8)&0xff;
蓝色整数=(像素)&0xff;
HSSFPalette调色板=wb.getCustomPalette();
HSSFCellStyle=wb.createCellStyle();
HSSFRow行=sheet.createRow((短)yy);
HSSFCell cell=row.createCell((短)xx);
cell.setCellValue(yy);
风格。setFillForegroundColor(HSSFColor.LIME.index);
风格.背景颜色(HSSFColor.LIME.index);
调色板.setColoratinex(HSSFColor.LIME.index,(字节)红色,(字节)绿色,(字节)蓝色);
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;
                 }

}
公共类引擎{
ArrayList arr=新的ArrayList();
私人int xx;
私人int yy;
文件输出流输出;
HSSF表;
HSSF工作手册wb;
r行;
public void process()引发AWTException,IOException{
wb=新的HSSF工作手册();
sheet=wb.createSheet();
wb.setActiveSheet(0);
BuffereImage img=null;
试一试{
img=ImageIO.read(新文件(“res/images.jpg”);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
System.out.println(“未找到img文件”);
}
对于(int x=0;x 24)&0xff;
int red=(像素>>16)和0xff;
绿色整数=(像素>>8)&0xff;
蓝色整数=(像素)&0xff;
单元c=r.createCell(yy);
HSSFCellStyle=wb.createCellStyle();
HSSFColor col=setColor(wb,(字节)红色,(字节)绿色,(字节)蓝色);
style.setFillPattern(HSSFCellStyle.SOLID\u前景);
setFillForegroundColor(col.getIndex());
c、 setCellStyle(风格);
}
公共HSSFColor setColor(HSSF工作簿,字节r,字节g,字节b){
HSSFPalette调色板=工作簿.getCustomPalette();
HSSFColor HSSFColor=null;
试一试{
hssfColor=调色板。查找相似颜色(r、g、b);
if(hssfColor==null){
色板颜色指数(HSSFColor.LAVENDER.index,r,g,b);
hssfColor=palete.getColor(hssfColor.LAVENDER.index);
}
}捕获(例外e){
System.out.println(“错误”);
}
返回hssfColor;
}
}