我是Java的新手,需要在Microsoft Excel单元格中添加注释图像 我的代码返回一个异常,任何人都可以帮忙吗? 我在Java中的代码
public static void main(String[] args) throws Exception{
/* Create a Workbook and Worksheet */
HSSFWorkbook my_workbook = new HSSFWorkbook();
HSSFSheet my_sheet = my_workbook.createSheet("MyBanner");
/* Read the input image into InputStream */
InputStream image = new FileInputStream("C:/Users/Administrator/Desktop/New folder/Excel/Jellyfish.jpg");
/* Convert Image to byte array */
byte[] bytes = IOUtils.toByteArray(image);
/* Add Picture to workbook and get a index for the picture */
int my_picture_id = my_workbook.addPicture(bytes, Workbook.PICTURE_TYPE_JPEG);
/* Close Input Stream */
image.close();
/* Create the drawing container */
HSSFPatriarch drawing = my_sheet.createDrawingPatriarch();
/* Create an anchor point */
ClientAnchor my_anchor = new HSSFClientAnchor();
/* Define top left corner, and we can resize picture suitable from there */
my_anchor.setCol1(2);
my_anchor.setRow1(1);
/* Invoke createPicture and pass the anchor point and ID */
HSSFPicture my_picture = drawing.createPicture(my_anchor, my_picture_id);
/* Call resize method, which resizes the image */
my_picture.resize();
/* Write changes to the workbook */
FileOutputStream out = new FileOutputStream(new File("C:/Users/Administrator/Desktop/New folder/Excel/Book1.xlsx"));
my_workbook.write(out);
out.close();
}
它给出了这个例外
线程“main”中的异常java.lang.NoClassDefFoundError:org / apache / commons / codec / digest / DigestUtils 在org.apache.poi.hssf.usermodel.HSSFWorkbook.addPicture(HSSFWorkbook.java:1575) 在app.App.main(App.java:39) 引起:java.lang.ClassNotFoundException:org.apache.commons.codec.digest.DigestUtils at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher $ AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ......还有2个 C:\ Users \ Administrator \ AppData \ Local \ NetBeans \ Cache \ 8.1 \ executor-snippets \ debug.xml:83:Java返回:1 BUILD FAILED(总时间:0秒)
如何解决?
答案 0 :(得分:0)
你也可以尝试这样的方式
final FileInputStream stream =
new FileInputStream( "C:/Users/Administrator/Desktop/New folder/Excel/Jellyfish.jpg" );
final CreationHelper helper = workbook.getCreationHelper();
final Drawing drawing = sheet.createDrawingPatriarch();
final ClientAnchor anchor = helper.createClientAnchor();
anchor.setAnchorType( ClientAnchor.MOVE_AND_RESIZE );
final int pictureIndex =
workbook.addPicture( stream, Workbook.PICTURE_TYPE_JPEG );
anchor.setCol1( 0 );
anchor.setRow1( LOGO_ROW ); // same row is okay
anchor.setRow2( LOGO_ROW );
anchor.setCol2( 1 );
final Picture pict = drawing.createPicture( anchor, pictureIndex );
pict.resize();