for int i = 0; i length; i++ r = sheetcreateRowi + rowcount; rsetHeightshort 256 2; int cellNum = 0; JSONObject temp = new JSONObjectdataoptStringi; for cellNum = 0; cellNum display
根据您提供的代码和问题描述,报错的原因可能是在创建单元格时出现错误。为了创建一个超链接单元格,您可以尝试使用HSSFHyperlink类来创建超链接对象,然后将其设置给单元格。以下是修改后的代码示例:
import org.apache.poi.common.usermodel.HyperlinkType;
import org.apache.poi.hssf.usermodel.HSSFHyperlink;
import org.apache.poi.ss.usermodel.CreationHelper;
for (int i = 0; i < length; i++) {
r = sheet.createRow(i + rowcount);
r.setHeight((short) (256 * 2));
int cellNum = 0;
JSONObject temp = new JSONObject(data.optString(i));
for (cellNum = 0; cellNum < displayKeys.length + 6; cellNum++) {
String value = temp.optString(dataKeys[cellNum]);
if (dataKeys[cellNum].equals("images")) {
JSONArray images = temp.optJSONArray("images");
if (images != null) {
for (int j = 0; j < images.length(); j++) {
String imageUrl = images.optString(j) + "?imageMogr2/thumbnail/!20p";
String imageHyperLinkString = "=HYPERLINK(\"" + imageUrl + "\")";
System.out.println(imageUrl);
System.out.println(imageHyperLinkString);
Cell cell = CellUtil.createCell(r, cellNum, value);
CreationHelper creationHelper = sheet.getWorkbook().getCreationHelper();
HSSFHyperlink hyperlink = (HSSFHyperlink) creationHelper.createHyperlink(HyperlinkType.URL);
hyperlink.setAddress(imageUrl);
cell.setHyperlink(hyperlink);
// 将imageUrl添加到allImageUrls中
allImageUrls += imageUrl + "\n";
}
}
} else {
CellUtil.createCell(r, cellNum, value);
}
}
}
这样,您就能够在指定的单元格中创建一个超链接,链接到指定的URL地址。请注意,这里使用的是HSSFHyperlink类,因此要确保您的代码使用的是HSSF(.xls)格式的Excel文件。如果您使用的是XSSF(.xlsx)格式的Excel文件,则需要使用XSSFHyperlink类来创建超链接对象
原文地址: http://www.cveoy.top/t/topic/iZkP 著作权归作者所有。请勿转载和采集!