@Stateless
public class GetExcel
implements GetExcelLocal
{
/**
* outputFile.
*/
public static String outputFile = "Excel.xls";
/**
* @param workbook
* .
*/
private HSSFWorkbook workbook;
/**
* sheet.
*/
private HSSFSheet sheet;
/**
* row.
*/
private HSSFRow row;
/**
* cell.
*/
private HSSFCell cell;
/**
* logger.
*/
private Log logger = LogFactory.getLog(this.getClass());
/**
* em.
*/
@PersistenceContext
private EntityManager em;
/**
* 插入单元
*
* @param cellIndex
* 列数
* @param context
* 内容
* */
public final void insertCell(int cellIndex, String context, CellStyle style)
{
cell = row.createCell(cellIndex);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(context);
cell.setCellStyle(style);
}
/**
* @author pan.
* @throws Exception
* e. return file inputStream.
* @param argPoolID
* .
*
*/
@Override
public final InputStream findExcle(final Long argPoolID)
{
logger.info("argPoolID value is***** " + argPoolID);
TDeQuestionPoolEnt pool = this.em.find(TDeQuestionPoolEnt.class, argPoolID);
int PoolAddressLength = pool.getTdeQuestionPoolAddresses().size();
logger.info("PoolAddressLength value is " + PoolAddressLength);
workbook = new HSSFWorkbook();
sheet = workbook.createSheet("题库excle表");
sheet.setDefaultColumnWidth(10);
//the stytl for excel
HSSFCellStyle style_1 = workbook.createCellStyle();
style_1.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 水平居中
style_1.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
for (int i = 0; i < PoolAddressLength; i++)
{
if (pool.getTdeQuestionPoolAddresses().get(i).getType().equals("REMOTE"))
continue;
else
{
if (pool.getTdeQuestionPoolAddresses().get(i).getName().toString()
.equals("QUESTION_DETAIL"))
{
row = sheet.createRow(2);
sheet.addMergedRegion(new Region(2, (short) (0), 2, (short) (1)));
sheet.addMergedRegion(new Region(2, (short) (2), 2, (short) (6)));
this.insertCell(0, "题目详情接口", style_1);
this.insertCell(2, pool.getTdeQuestionPoolAddresses().get(i).getUrl(), style_1);
} else if (pool.getTdeQuestionPoolAddresses().get(i).getName().toString()
.equals("QUESTION_CONTENT"))
{
row = sheet.createRow(0);
sheet.addMergedRegion(new Region(0, (short) (0), 0, (short) (1)));
sheet.addMergedRegion(new Region(0, (short) (2), 0, (short) (6)));
this.insertCell(0, "题目内容接口", style_1);
this.insertCell(2, pool.getTdeQuestionPoolAddresses().get(i).getUrl(), style_1);
} else if (pool.getTdeQuestionPoolAddresses().get(i).getName().toString()
.equals("QUESTION_ID"))
{
row = sheet.createRow(1);
sheet.addMergedRegion(new Region(1, (short) (0), 1, (short) (1)));
sheet.addMergedRegion(new Region(1, (short) (2), 1, (short) (6)));
this.insertCell(0, "题目ID接口", style_1);
String params = "?poolId=";
this.insertCell(2, pool.getTdeQuestionPoolAddresses().get(i).getUrl()+params+argPoolID, style_1);
}
}
}
row = sheet.createRow(3);
this.insertCell(0, "标准属性名 ", style_1);
this.insertCell(1, "标准属性别名 ", style_1);
this.insertCell(2, "对应属性名 ", style_1);
this.insertCell(3, "对应属性别名 ", style_1);
sheet.addMergedRegion(new Region(3, (short) (4), 3, (short) (7)));
this.insertCell(4, " 标准值 对应值 ", style_1);
int k = 0;
int v = 4;
for (int i = 0; i < pool.getTdeConditionTypes().size(); i++)
{
int ConditionValueSize = pool.getTdeConditionTypes().get(i).getTdeConditionValues().size();
k = k + ConditionValueSize;
sheet.addMergedRegion(new Region(v, (short) (0), 3 + k, (short) (0)));
sheet.addMergedRegion(new Region(v, (short) (1), 3 + k, (short) (1)));
sheet.addMergedRegion(new Region(v, (short) (2), 3 + k, (short) (2)));
sheet.addMergedRegion(new Region(v, (short) (3), 3 + k, (short) (3)));
row = sheet.createRow(v);
int a = v;
v = 4 + k;
this.insertCell(0, pool.getTdeConditionTypes().get(i).getLocalTypeName().toString(), style_1);
this.insertCell(1, pool.getTdeConditionTypes().get(i).getLocalType().toString(), style_1);
this.insertCell(2, pool.getTdeConditionTypes().get(i).getRemoteTypeName().toString(), style_1);
this.insertCell(3, pool.getTdeConditionTypes().get(i).getRemoteType().toString(), style_1);
for (int j = 0; j < ConditionValueSize; j++)
{
this.insertCell(4, pool.getTdeConditionTypes().get(i).getTdeConditionValues().get(j)
.getLocalTypeValueName().toString(), style_1);
this.insertCell(5, pool.getTdeConditionTypes().get(i).getTdeConditionValues().get(j)
.getLocalTypeValue().toString(), style_1);
this.insertCell(6, pool.getTdeConditionTypes().get(i).getTdeConditionValues().get(j)
.getRemoteTypeValueName().toString(), style_1);
this.insertCell(7, pool.getTdeConditionTypes().get(i).getTdeConditionValues().get(j)
.getRemoteTypeValue().toString(), style_1);
a++;
row = sheet.createRow(a);
}
}
try
{
FileOutputStream fOut = new FileOutputStream(
outputFile);
workbook.write(fOut);
fOut.flush();
fOut.close();
File tmpFile = new File(outputFile);
return new FileInputStream(tmpFile);
} catch (Exception e)
{
return null;
}
//
// ByteArrayOutputStream out;
// out = new ByteArrayOutputStream();
// try
// {
// workbook.write(out);
// return new ByteArrayInputStream(out.toByteArray());
// } catch (IOException e)
// {
// e.printStackTrace();
// }
// return null;
// }
}
DownExcelAction
public class DownloadExcleAction
extends ActionSupport
{
/**
* serialVersionUID.
*/
private static final long serialVersionUID = -5702072412578248219L;
/**
* inputPath.
*/
private String inputPath;
/**
* fileName.
*/
private String fileName;
/**
* logger.
*/
private Log logger = LogFactory.getLog(this.getClass());
/**
* poolId.
*/
private Long poolId;
/**
* downLoadFile.
*/
private InputStream downLoadFile;
/**
* excel.
*/
private GetExcelLocal excel = JndiTools.bean("GetExcel", GetExcelLocal.class);
/**
* @param argDownLoadFile
* the downLoadFile to set
*/
public final void setDownLoadFile(
final InputStream argDownLoadFile)
{
this.downLoadFile = argDownLoadFile;
}
/**
* 获得下载文件.
*
* @return 下载文件流
*/
public final InputStream getDownLoadFile()
{
return downLoadFile;
}
/**
*
* @return input.
*
* @author Administrator
*/
public final String getInputPath()
{
return inputPath;
}
/**
* @param argInputPath
* 文件路径
*/
public final void setInputPath(final String argInputPath)
{
this.inputPath = argInputPath;
}
/**
*
* @param argFileName
* 文件名
*/
public final void setFileName(final String argFileName)
{
this.fileName = argFileName;
}
/**
*
* @return
* @throws UnsupportedEncodingException .
*
* @author Administrator
*/
public final String getFileName()
throws UnsupportedEncodingException
{
String downFileName;
downFileName = new String(fileName.getBytes("GBK"),
"ISO-8859-1");
logger.info(downFileName);
return downFileName;
}
/**
*
* @param filename
* @throws UnsupportedEncodingException .
*
* @author pan.
*/
public final String encode(final String filename)
throws UnsupportedEncodingException
{
return new String(filename.getBytes("ISO-8859-1"),
"GBK");
}
/**
* @return success,error
* @author Administrator.
* @throws Exception
* e.
*
*
*/
@Override
public final String execute() throws Exception
{
SimpleDateFormat formator = new SimpleDateFormat(
"yyyy-MM-dd hh:mm");
Date date = new Date();
if(fileName==null||fileName.trim().isEmpty()){
fileName = "excle表" + formator.format(date) + ".xls";
}else{
fileName += ".xls";
}
// Long poolId = 1060L;
try
{
logger.info("POOLID VALUE IS " + poolId);
downLoadFile = excel.findExcle(poolId);
} catch (Exception e)
{
logger.info("GET THE SERVICE FAIL!");
e.printStackTrace();
}
return SUCCESS;
}
/**
* @return the poolId .
*
* @author Administrator
*/
public final Long getPoolId()
{
return this.poolId;
}
/**
* @param argPoolId
* the poolId to set .
*
* @author Administrator
*/
public final void setPoolId(final Long argPoolId)
{
this.poolId = argPoolId;
}
}
struts 配置
<action name="downloadQuestionPoolExcel" class="org.r.DownloadExcleAction">
<result name="success" type="stream">
<param name="inputName">downLoadFile</param>
<param name="contentDisposition">attachment;filename="${fileName}"</param>
<param name="bufferSize">4096</param>
</result>
<result name="resultnull">error.jsp</result>
</action>