您好,模板暂时不支持url类型的图片,但是您可以通过下面的代码,将url地址转换为base64
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.UnsupportedEncodingException;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLEncoder;
- import com.grapecity.documents.excel.Workbook;
- import com.grapecity.documents.excel.drawing.IShape;
- public class Test {
- public static void main(String[] args) throws MalformedURLException, UnsupportedEncodingException {
- Workbook workbook = new Workbook();
- workbook.open("Bug_4098.json");
- IShape pic = workbook.getActiveSheet().getShapes().get(0);
- String url = pic.getPictureFormat().getUrl();
- byte[] picByte = GetPicFromUrl(url);
- pic.getPictureFormat().setFill(picByte);
- workbook.save("Bug_4098.pdf");
- workbook.save("Bug_4098.xlsx");
- }
- private static byte[] GetPicFromUrl(String urlString) throws MalformedURLException, UnsupportedEncodingException {
- URL url = new URL(encode(urlString));
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- try (InputStream inputStream = url.openStream()) {
- int n = 0;
- byte[] buffer = new byte[1024];
- while (-1 != (n = inputStream.read(buffer))) {
- baos.write(buffer, 0, n);
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- return baos.toByteArray();
- }
- private static String encode(String url) throws UnsupportedEncodingException {
- char[] charArray = url.toCharArray();
- StringBuilder sb = new StringBuilder();
- for (char c : charArray) {
- if (c >= 0 && c < 255) {
- sb.append(c);
- } else {
- sb.append(URLEncoder.encode(String.valueOf(c), "UTF-8"));
- }
- }
- return sb.toString();
- }
- }
复制代码
|