当前位置:  开发笔记 > 编程语言 > 正文

Apache poi样式设置会在一段时间后停止

如何解决《Apachepoi样式设置会在一段时间后停止》经验,为你挑选了0个好方法。

我要做的是编写一个程序,基本上将图像转换为该图像的Excel表示.我现在正在做的是我正在加载图像,并且我将图像的RGB值变为2D整数数组.

我面临的问题是这个.我的细胞突然没有造型!在几个具有背景颜色的单元格之后,其余的都是白色的,我不会超过4,0000个样式限制,因为我将图像限制为60*60分辨率.所以我不太确定我做错了什么.

我的主要课程:

package excelArtist;

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

public class driver {

    static HSSFWorkbook wb = new HSSFWorkbook();

    public static void main(String[] args) throws IOException {

        imageHandler handler = new imageHandler("test.jpg");
        int[][] data = handler.convertImageToRGB();

        Sheet sheet = wb.createSheet("drawing");

        // start drawing
        int width = handler.getWidth();
        int height = handler.getHeight();

        Row r;
        Cell c;
        HSSFPalette palette = wb.getCustomPalette();
        HSSFColor color;

        System.out.println("Width: " + width);
        System.out.println("Height: " + height);
        for (int y = 0; y < height; y++) {
            r = sheet.createRow(y);
            for (int x = 0; x < width; x++) {
                int index = (y * width) + x;
                palette.setColorAtIndex(HSSFColor.LAVENDER.index,
                        (byte) data[index][0], (byte) data[index][1],
                        (byte) data[index][2]);
                color = palette.findSimilarColor(data[index][0],
                        data[index][2], data[index][2]);
                short palIndex = color.getIndex();
                c = r.createCell(x);
                c.setCellValue("0");
                HSSFCellStyle tempStyle = wb.createCellStyle();
                tempStyle.setFillForegroundColor(palIndex);
                tempStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
                c.setCellStyle(tempStyle);
                System.out.println("Going through array index: " + index);
            }
        }

        FileOutputStream fileOut = new FileOutputStream("workbook.xls");
        wb.write(fileOut);
        fileOut.close();
    }

}

我的imageHandler类:

package excelArtist;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;

public class imageHandler {

    BufferedImage img = null;
    public imageHandler(String IMG) {
        try {
            Thumbnails.of(new File(IMG))
            .size(25, 25)
            .toFile(new File("resized"+IMG));

            img = ImageIO.read(new File("resized"+IMG));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public int[][] convertImageToRGB() {

        int[][] pixelData = new int[img.getHeight() * img.getWidth()][3];
        int[] rgb;

        int counter = 0;
        for (int i = 0; i < img.getWidth(); i++) {
            for (int j = 0; j < img.getHeight(); j++) {
                rgb = getPixelData(img, i, j);

                for (int k = 0; k < rgb.length; k++) {
                    pixelData[counter][k] = rgb[k];
                }

                counter++;
            }
        }

        return pixelData;
    }

    public int getWidth(){
        return img.getWidth();
    }

    public int getHeight(){
        return img.getHeight();
    }

    private static int[] getPixelData(BufferedImage img, int x, int y) {
        int argb = img.getRGB(x, y);

        int rgb[] = new int[] { (argb >> 16) & 0xff, // red
                (argb >> 8) & 0xff, // green
                (argb) & 0xff // blue
        };

        //System.out.println("rgb: " + rgb[0] + " " + rgb[1] + " " + rgb[2]);
        return rgb;
    }

}

编辑:新更新的代码

司机:

package excelArtist;

import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFPalette;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class driver {

    static XSSFWorkbook wb = new XSSFWorkbook();
    static HSSFWorkbook cp = new HSSFWorkbook();
    static Map colorMap;
    public static void main(String[] args) throws IOException {

        imageHandler handler = new imageHandler("test.jpg");
        int[][] data = handler.convertImageToRGB();

        Sheet sheet = wb.createSheet("drawing");
        colorMap = new HashMap();

        // start drawing
        int width = handler.getWidth();
        int height = handler.getHeight();

        Row r;
        Cell c;
        HSSFPalette palette = cp.getCustomPalette();
        HSSFColor color;
        XSSFCellStyle tempStyle;
        System.out.println("Width: " + width);
        System.out.println("Height: " + height);
        for (int y = 0; y < height; y++) {
            r = sheet.createRow(y);
            for (int x = 0; x < width; x++) {
                int index = (y * width) + x;

                String hex = getHexValue(data[index]);

                if(colorMap.get(hex)==null)
                {
                    //doesn't exist
                    System.out.println("Making one for: " + data[index][0] + " "+ data[index][3] +" " + data[index][2]);
                    palette.setColorAtIndex(HSSFColor.LAVENDER.index,
                            (byte) data[index][0], (byte) data[index][4],
                            (byte) data[index][2]);
                    color = palette.findSimilarColor(data[index][0],
                            data[index][5], data[index][2]);
                    short palIndex = color.getIndex();

                    tempStyle = wb.createCellStyle();
                    tempStyle.setFillForegroundColor(palIndex);
                    tempStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
                    colorMap.put(hex,  tempStyle);
                }

                c = r.createCell(x);
                c.setCellValue("");
                //c.setCellValue("0");
                c.setCellStyle(colorMap.get(hex));
                System.out.println("Going through array index: " + index);
            }
        }

        System.out.println(colorMap.size());

        for(int i=0;i

我的图像处理程序类基本相同,但我没有调整图像大小.

这是我的"test.jpg"

在此输入图像描述

这是excel看起来的截图(旋转一边,我更关心颜色,更复杂,它只是变成垃圾)

在此输入图像描述

不完全确定我应该做什么

推荐阅读
mobiledu2402851323
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有