Java 用图像生成Excel表格
我正在尝试使用Netbeans使用Java生成excel工作表,我希望excel工作表包含可以使用JFileChooser添加的图像,方法是将其解析为数组中的字节并将其放入列A中,而列B是该图像的描述,但是我编写的代码只在B列中放置了一个图像,而我想在不同的行中列出多个图像,并在B列中对它们进行描述 主要类别:Java 用图像生成Excel表格,java,apache-poi,Java,Apache Poi,我正在尝试使用Netbeans使用Java生成excel工作表,我希望excel工作表包含可以使用JFileChooser添加的图像,方法是将其解析为数组中的字节并将其放入列A中,而列B是该图像的描述,但是我编写的代码只在B列中放置了一个图像,而我想在不同的行中列出多个图像,并在B列中对它们进行描述 主要类别: import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.ClientAnchor;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.Drawing;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.Picture;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelWriter {
static String imagePath;
static String imageDesc;
static int numOfData;
private static String[] columns = { "DropBox Link", "Description"};
private static List<Data> datas = new ArrayList<Data>();
static JFileChooser chooser = new JFileChooser();
public static void main(String[] args) throws IOException,
InvalidFormatException {
//taking user input using Scanner Library
Scanner in = new Scanner(System.in);
System.out.println("How many data you need?");
numOfData = in.nextInt();
// numOfData = numOfData+1;
for (int i = 0; i < numOfData; i++) {
System.out.println("Choose the file");
File file = chooser.getSelectedFile();
chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int returnVal = chooser.showOpenDialog(chooser);
if(returnVal == JFileChooser.APPROVE_OPTION) {
System.out.println("You chose to open this directory: " +
chooser.getSelectedFile().getAbsolutePath());
}
imagePath = chooser.getSelectedFile().getAbsolutePath();
System.out.println("Description for it?");
imageDesc = JOptionPane.showInputDialog("Input");
datas.add(new Data(imagePath, imageDesc));
}
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("dropboxLinks");
//FileInputStream obtains input bytes from the image file
InputStream inputStream = new FileInputStream(chooser.getSelectedFile().getAbsolutePath());
//Get the contents of an InputStream as a byte[].
byte[] bytes = IOUtils.toByteArray(inputStream);
//Adds a picture to the workbook
int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
//close the input stream
inputStream.close();
//Returns an object that handles instantiating concrete classes
CreationHelper helper = workbook.getCreationHelper();
//Creates the top-level drawing patriarch.
Drawing drawing = sheet.createDrawingPatriarch();
//Create an anchor that is attached to the worksheet
ClientAnchor anchor = helper.createClientAnchor();
//create an anchor with upper left cell _and_ bottom right cell
anchor.setCol1(1); //Column B
anchor.setRow1(2); //Row 3
anchor.setCol2(2); //Column C
anchor.setRow2(3); //Row 4
//Creates a picture
Picture pict = drawing.createPicture(anchor, pictureIdx);
//Reset the image to the original size
//pict.resize(); //don't do that. Let the anchor resize the image!
//Create the Cell B3
//Cell cell = sheet.createRow(2).createCell(1);
Font headerFont = workbook.createFont();
headerFont.setBold(true);
headerFont.setFontHeightInPoints((short) 14);
headerFont.setColor(IndexedColors.RED.getIndex());
CellStyle headerCellStyle = workbook.createCellStyle();
headerCellStyle.setFont(headerFont);
// Create a Row
Row headerRow = sheet.createRow(0);
for (int i = 0; i < columns.length; i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(columns[i]);
cell.setCellStyle(headerCellStyle);
}
// Create Other rows and cells with datas data
int rowNum = 1;
for (Data datas : datas) {
Row row = sheet.createRow(rowNum++);
row.createCell(0).setCellValue(datas.imagePath);
row.createCell(1).setCellValue(datas.imageDesc);
}
// Resize all columns to fit the content size
for (int i = 0; i < columns.length; i++) {
sheet.autoSizeColumn(i);
}
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("dropboxLinks.xlsx");
workbook.write(fileOut);
fileOut.close();
}
}
我希望在第一行有一个标题,代码从第二行开始,使用JFileChooser在a列中放置一个图像,在B列中我希望它是在a列中使用JOptionPane.showInputDialog()对该图像的描述。您从未对
图像进行迭代,仅对imagePath
和imageDescription
执行迭代
在代码中:
InputStream InputStream=新文件InputStream(chooser.getSelectedFile().getAbsolutePath())
这是最后一次输入,因此您的excel只有最后一张图片
更正此处以解决您的问题:
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("dropboxLinks");
int startRow = 1;
for(int intR=0; intR<datas.size(); intR++) {
//FileInputStream obtains input bytes from the image file
InputStream inputStream = new FileInputStream(datas.get(intR).imagePath);
//Get the contents of an InputStream as a byte[].
byte[] bytes = org.apache.poi.util.IOUtils.toByteArray(inputStream);
//Adds a picture to the workbook
int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
//close the input stream
inputStream.close();
//Returns an object that handles instantiating concrete classes
CreationHelper helper = workbook.getCreationHelper();
//Creates the top-level drawing patriarch.
Drawing drawing = sheet.createDrawingPatriarch();
//Create an anchor that is attached to the worksheet
ClientAnchor anchor = helper.createClientAnchor();
//create an anchor with upper left cell _and_ bottom right cell
anchor.setCol1(1); //Column B
anchor.setRow1(startRow);
anchor.setCol2(2); //Column C
anchor.setRow2(3); //Row 4
//Creates a picture
Picture pict = drawing.createPicture(anchor, pictureIdx);
startRow++;
}
//Reset the image to the original size
//pict.resize(); //don't do that. Let the anchor resize the image!
//Create the Cell B3
//Cell cell = sheet.createRow(2).createCell(1);
Workbook工作簿=新建XSSF工作簿();
Sheet Sheet=workbook.createSheet(“dropboxLinks”);
int startRow=1;
对于(int intR=0;intR您从未对图像进行迭代
,您只对图像路径
和图像描述
进行迭代
在代码中:
InputStream InputStream=新文件InputStream(chooser.getSelectedFile().getAbsolutePath())
这是最后一次输入,因此您的excel只有最后一张图片
更正此处以解决您的问题:
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("dropboxLinks");
int startRow = 1;
for(int intR=0; intR<datas.size(); intR++) {
//FileInputStream obtains input bytes from the image file
InputStream inputStream = new FileInputStream(datas.get(intR).imagePath);
//Get the contents of an InputStream as a byte[].
byte[] bytes = org.apache.poi.util.IOUtils.toByteArray(inputStream);
//Adds a picture to the workbook
int pictureIdx = workbook.addPicture(bytes, Workbook.PICTURE_TYPE_PNG);
//close the input stream
inputStream.close();
//Returns an object that handles instantiating concrete classes
CreationHelper helper = workbook.getCreationHelper();
//Creates the top-level drawing patriarch.
Drawing drawing = sheet.createDrawingPatriarch();
//Create an anchor that is attached to the worksheet
ClientAnchor anchor = helper.createClientAnchor();
//create an anchor with upper left cell _and_ bottom right cell
anchor.setCol1(1); //Column B
anchor.setRow1(startRow);
anchor.setCol2(2); //Column C
anchor.setRow2(3); //Row 4
//Creates a picture
Picture pict = drawing.createPicture(anchor, pictureIdx);
startRow++;
}
//Reset the image to the original size
//pict.resize(); //don't do that. Let the anchor resize the image!
//Create the Cell B3
//Cell cell = sheet.createRow(2).createCell(1);
Workbook工作簿=新建XSSF工作簿();
Sheet Sheet=workbook.createSheet(“dropboxLinks”);
int startRow=1;
对于(int intR=0;intRSo,这里有什么问题?问题是我希望图像显示在A列中,但它显示在B列中,无法插入多个图像。那么,这里有什么问题?问题是我希望图像显示在A列中,但它显示在B列中,无法插入多个图像。非常感谢您的回复,这是e这对我非常有帮助,但问题还没有完全解决,现在它显示了2个图像,即使用户希望显示更多,请参考上面的代码System.out.println(“需要多少数据?”);numOfData=in.nextInt();
因此,如果用户需要6个数据,并且他选择了6个图像,那么只有其中的2个将显示在第2行和第3行的工作表中。非常感谢您的回复,这对我非常有帮助,但问题还没有完全解决,现在它显示了2个图像,即使用户希望显示更多图像,请参考上面的代码System.out.println(“您需要多少数据?”);numfdata=in.nextInt();
因此,如果用户需要6个数据,并且他选择了6个图像,则只有其中的2个图像将显示在第2行和第3行的工作表中。