我需要嵌入文件
(.ppt(x), .doc(x), .jpg, .txt,...)
在excel (format xlsx)
中使用Java Apache POI。
我找到了一个使用excel(格式xls)嵌入文件的示例
POI-HSSF
(Embed files into Excel using Apache POI),
但此示例不适用于excel xlsx格式。
有人知道是否可以使用POI-XSSF
?
答案 0 :(得分:0)
'尝试使用它对我有用...'使用这个罐子运行此代码..... 1)poi-3.13-20150929.jar 2)poi-ooxml-3.13-20150929.jar 3)poi-ooxml-schemas-3.13-20150929.jar
public String getReport() throws RecordNotFoundException{
ResultSet rs = null;
ResultSet rs1 = null;
Connection conn = null;
CallableStatement cstm = null;
PreparedStatement pstmt = null;
try {
HttpServletRequest request;
HttpSession session;
request = (HttpServletRequest) ActionContext.getContext().get(
"com.opensymphony.xwork2.dispatcher.HttpServletRequest");
session = request.getSession();
conn = JndiConnection.getOracleConnection();
String sReportName = "";
ArrayList<String> mainData = new ArrayList<String>();
conn = JndiConnection.getOracleConnection();
cstm = conn.prepareCall("your procedure name");
cstm.setString(1, first parameter);
cstm.setString(2, second parameter);
cstm.registerOutParameter(3, OracleTypes.CURSOR);
cstm.execute();
rs1 = (ResultSet) cstm.getObject(3);
ResultSetMetaData rsmd = rs1.getMetaData();
String sColumnData="Sr.No";
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
sColumnData+="~~" + rsmd.getColumnName(i);
}
String headerString=sColumnData.replace("Sr.No~~", "");
mainData.add(headerString);
while (rs1.next()) {
System.out.println("data...............::" + rs1.getString(1));
String sData = "data";
for (int i = 1; i <= rsmd.getColumnCount(); i++) {
String column=rs1.getString(rsmd.getColumnName(i));
if (column == null) {
String finalData = "";
sData += "~~" + finalData;
} else {
sData += "~~" + rs1.getString(rsmd.getColumnName(i));
}
}
String bodyData=sData.replace("data~~", "");
mainData.add(bodyData);
System.out.println();
}
System.out.println("main data...............::" + mainData);
session.setAttribute("myReportData", mainData);
sReportName = genReport(mainData, title in file, "file path");
if (!"".equals(sReportName)) {
session.setAttribute("myReportDataExcelFile", sReportName);
}
setFileInputStream(new BufferedInputStream(new FileInputStream(sReportName)));
setFileName(new File(sReportName).getName());
} catch (SQLException se) {
throw new RecordNotFoundException(se);
} catch (NestableException ne) {
throw new RecordNotFoundException(ne);
} catch (Exception e) {
throw new RecordNotFoundException(e);
} finally {
try {
JndiConnection.freeConnections(cstm, conn, rs, rs1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return SUCCESS;
}"
'Above方法将从数据库中获取数据库并将其放入列表,现在使用波纹管方法 用POI HSSF编写xls文件。'
private String genReport(ArrayList<String> arrDisplayDataList, String FileName, String sTitleName, String sPath)
throws Exception {
String fileNameWithPath;
Exception exception;
int col = 0;
int sno = 1;
fileNameWithPath = "";
String sFromate = "";
Connection con = null;
String ColumnDataList[] = null;
System.out.println("::::: Inside genReport() 2:::::");
try {
String[] arrColumnList = arrDisplayDataList.get(0).split("~~");
System.out.println("Length:::" + arrColumnList.length);
if (arrColumnList.length > 1) {
System.out.println("==Inside Of Record Data===");
DateFormat dateFormat = new SimpleDateFormat("ddMMyyyyHHmmss");
Calendar cal = Calendar.getInstance();
sFromate = dateFormat.format(cal.getTime());
FileName = (new StringBuilder()).append(FileName).append("_").append(sFromate).toString();
fileNameWithPath = (new StringBuilder()).append(sPath).append("/").append(FileName).append(".xls")
.toString();
File f1 = new File((new StringBuilder()).append(sPath).append("/").append(FileName).append(".xls")
.toString());
f1.getParentFile().mkdirs();
HSSFWorkbook wb = new HSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream(f1);
HSSFSheet sheets = wb.createSheet("Sheet" + (sno++));
int row = 1;
HSSFRow rows;
if (row == 1) {
rows = sheets.createRow(0);
HSSFCell cells = rows.createCell(0);
cells.setCellType(1);
HSSFCellStyle cellStyle1 = wb.createCellStyle();
cellStyle1.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle1.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle1.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle1.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cells.setCellStyle(cellStyle1);
cells.setCellValue(sTitleName);
HSSFCellStyle cellStyle = wb.createCellStyle();
HSSFFont fontStyle = wb.createFont();
cellStyle.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
fontStyle.setColor(IndexedColors.BLACK.getIndex());
fontStyle.setBoldweight((short) 700);
fontStyle.setFontHeightInPoints((short) 12);
cellStyle.setFillPattern((short) 1);
cellStyle.setFont(fontStyle);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER_SELECTION);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cells.setCellStyle(cellStyle);
sheets.addMergedRegion(new CellRangeAddress(0, 0, 0, arrColumnList.length - 1));
for (int columnIndex = 0; columnIndex < 50; columnIndex++) {
sheets.autoSizeColumn(columnIndex);
}
}
rows = sheets.createRow(row++);
for (col = 0; col < arrColumnList.length; col++) {
HSSFCell cells = rows.createCell(col);
String sVal = (String) arrColumnList[col];
cells.setCellType(1);
cells.setCellValue(sVal);
HSSFCellStyle cellStyle = wb.createCellStyle();
HSSFFont fontStyle = wb.createFont();
fontStyle.setColor(IndexedColors.BLACK.getIndex());
fontStyle.setBoldweight((short) 700);
fontStyle.setFontHeightInPoints((short) 10);
cellStyle.setFont(fontStyle);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
cells.setCellStyle(cellStyle);
for (int columnIndex = 0; columnIndex < 100; columnIndex++) {
sheets.autoSizeColumn(columnIndex);
}
}
int rownum = 2;
int rowcount = 0;
for (int i = 1; i < arrDisplayDataList.size(); i++) {
if (rownum >= 65000) {
sheets = wb.createSheet("Sheet " + (sno++));
rownum = 0;
}
rows = sheets.createRow(rownum++);
String sData = ((String) arrDisplayDataList.get(i)).toString();
String sSplitData[] = sData.split("~~");
int xcount = 0;
for (col = 0; col < sSplitData.length; col++) {
HSSFCell cells = rows.createCell(xcount++);
String sVal = sSplitData[col];
if (sVal.equalsIgnoreCase("Total")) {
HSSFCellStyle cellStyle = wb.createCellStyle();
HSSFFont fontStyle = wb.createFont();
fontStyle.setColor(IndexedColors.BLACK.getIndex());
fontStyle.setBoldweight((short) 600);
fontStyle.setFontHeightInPoints((short) 12);
cellStyle.setFont(fontStyle);
cellStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderTop(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderRight(HSSFCellStyle.BORDER_THIN);
cellStyle.setBorderLeft(HSSFCellStyle.BORDER_THIN);
// cells.setCellStyle(cellStyle);
cells.setCellType(1);
for (int k = 3; k < rows.getLastCellNum(); k++) {// For
// each
// cell
// in
// the
// row
rows.getCell(k).setCellStyle(cellStyle);// Set
// the
// sty;e
}
cells.setCellValue(sVal);
System.out.println("TTT:" + sVal);
} else {
/*
* HSSFCellStyle cellStyle = wb.createCellStyle();
* cellStyle
* .setBorderBottom(HSSFCellStyle.BORDER_THIN);
* cellStyle
* .setBorderTop(HSSFCellStyle.BORDER_THIN);
* cellStyle
* .setBorderRight(HSSFCellStyle.BORDER_THIN);
* cellStyle
* .setBorderLeft(HSSFCellStyle.BORDER_THIN);
* //cells.setCellStyle(cellStyle);
* cells.setCellType(1);
*/
cells.setCellValue(sVal);
}
}
}
wb.write(fileOut);
fileOut.close();
} else {
fileNameWithPath = "NO RECORD FOUND";
}
} catch (Exception e) {
System.out.println("Exception ::::" + e);
e.printStackTrace();
throw e;
}
return fileNameWithPath;
}