翻譯|使用教程|編輯:李顯亮|2021-04-12 10:34:03.933|閱讀 346 次
概述:本文將演示如何使用Spire.XLS for Java在Excel文件中插入和刪除形狀。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Spire.XLS for Java是專業的Java Excel API,使開發人員無需使用Microsoft Office或Microsoft Excel即可創建,管理,操作,轉換和打印Excel工作表。
本文將演示如何使用Spire.XLS for Java在Excel文件中插入和刪除形狀。
import com.spire.xls.*; import com.spire.xls.core.*; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; public class addShapestoExcel { public static void main(String[] args) throws Exception { String output = "output/AddShapesToExcelSheet.xlsx"; //create a workbook. Workbook workbook = new Workbook(); //get the first worksheet. Worksheet sheet = workbook.getWorksheets().get(0); //add a triangle shape. IPrstGeomShape triangle = sheet.getPrstGeomShapes().addPrstGeomShape(2, 2, 100, 100, PrstGeomShapeType.Triangle); //fill the triangle with solid color. triangle.getFill().setForeColor( Color.YELLOW); triangle.getFill().setFillType( ShapeFillType.SolidColor); //add a heart shape. IPrstGeomShape heart = sheet.getPrstGeomShapes().addPrstGeomShape(2, 5, 100, 100, PrstGeomShapeType.Heart); //fill the heart with gradient color. heart.getFill().setForeColor(Color.RED); heart.getFill().setFillType(ShapeFillType.Gradient); //add an arrow shape with default color. IPrstGeomShape arrow = sheet.getPrstGeomShapes().addPrstGeomShape(10, 2, 100, 100, PrstGeomShapeType.CurvedRightArrow); //add a cloud shape. IPrstGeomShape cloud = sheet.getPrstGeomShapes().addPrstGeomShape(10, 5, 100, 100, PrstGeomShapeType.Cloud); //fill the cloud with custom picture BufferedImage image = ImageIO.read(new File("SpireXls.png")); cloud.getFill().customPicture(image, "SpireXls.png"); cloud.getFill().setFillType( ShapeFillType.Picture); //save to file. workbook.saveToFile(output, ExcelVersion.Version2013); } }
輸出如下:
import com.spire.xls.*; public class removeShape { public static void main(String[] args) throws Exception { //Load the sample file Workbook workbook = new Workbook(); workbook.loadFromFile("output/AddShapesToExcelSheet.xlsx"); //get the first worksheet. Worksheet sheet = workbook.getWorksheets().get(0); //delete the second shape in the worksheet sheet.getPrstGeomShapes().get(1).remove(); /* //delete all shapes in the worksheet for (int i = sheet.getPrstGeomShapes().getCount()-1; i >= 0; i--) { sheet.getPrstGeomShapes().get(i).remove(); }*/ //save to file. workbook.saveToFile("output/RemoveParticularShape.xlsx", ExcelVersion.Version2013); } }
從Excel工作表中刪除第二個形狀后的有效屏幕截圖:
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn