问题描述
正如标题所暗示的,我需要把一些数据(我从数据库GOT)到Excel表格中,然后将其发送给客户端,使用户可以保存,打开或取消操作。
我看到一些文章对此,最近的一个是:。在提到史蒂文斯我尝试了以下code提供的链接:
公共字符串的execute(){
setContentDisposition(附件;文件名= \\+的ename +\\);
尝试{
ServletContext的参数servletContext = ServletActionContext.getServletContext();
字符串文件路径=的ServletContext.getRealPath(/ WEB-INF /模板/ EXCEL / mytemplate.xls);
档案文件=新的文件(文件路径);
工作簿WB = WorkbookFactory.create(新的FileInputStream(文件));
苫布苫布= wb.getSheetAt(0);
ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
wb.write(BAOS);
InputStream的excelStream;
excelStream =新ByteArrayInputStream进行(baos.toByteArray());
}赶上(例外五){
的System.out.println(e.getMessage());
}
返回成功;
}
下面首先 WorkbookFactory
没有定义。其次,我无法正确地理解如何code正常工作。
我也发现了此链接:http://www.roseindia.net/answers/viewqa/Java-Beginners/14930-How-to-export-data-from-database-to-excel-sheet-by-using-java--in-standalone-project.html.但这里的Excel文件被保存在服务器上。我想该文件不应该被保存在服务器端,就应该直接到客户端
(如果它帮助)我使用:Struts 2框架,休眠
我愿意使用诸如POI API,jQuery的或任何其他的好东西其他东西。
我不能使用 displaytag标签
出于某种原因。
使用Javascript将是我最后的手段(尽管我与它实现的),因为它需要更改浏览器的某些默认安全设置(如果这样可以避免我打开JavaScript的为好)。
请指教我应该怎么去这了。
谢谢!
编辑:
<结果类型>
<结果类型名称=JSP级=org.apache.struts2.views.jsp/>
<结果类型名称=砖类=org.apache.struts2.views.tiles.TilesResult/>
<结果类型名称=流级=org.apache.struts2.dispatcher.StreamResult/>
< /结果类型>
<作用NAME =myActionName级=package.myActionClass>
<结果类型=流>
< PARAM NAME =的contentType>中的应用程序/ vnd.ms - Excel的< /参数>
< PARAM NAME =inputName> excelStream< /参数>
< PARAM NAME =contentDisposition> contentDisposition< /参数>
< PARAM NAME =缓冲区大小> 1024 LT; /参数>
< /&结果GT;
< /作用>
在执行操作的错误:
java.lang.reflect.InvocationTargetExceptionjava.lang.IncompatibleClassChangeError:类org.apache.poi.hssf.usermodel.HSSFWorkbook没有实现请求的接口org.apache.poi.ss.usermodel.Workbook
好吧。所以最后我所有的路障是通过并已想出一个办法做到这一点。
我意识到,我所面临的问题不是在创建Excel文件时,这个问题是把它发送到客户端,而那也是没有在服务器上创建一个文件或临时文件。
因此,这里是如何去了解它(我从我原来的code扯掉细节,让您可以轻松地理解它)。
在操作文件你首先必须创建一个HSSFWorkbook对象时,把它的数据,然后没有它保存到磁盘上的服务器,使用的InputStream将其发送给客户端。
操作文件code:
公共字符串的execute(){ setContentDisposition(附件;文件名= \\+ + ENAME的.xls \\); 尝试{
HSSFWorkbook HWB =新HSSFWorkbook();
HSSFSheet表= hwb.createSheet(新表); //////您可以重复使用,或同时创建多个行这部分//////
HSSFRow行= sheet.createRow(的rowNum);
row.createCell(0).setValue(COL0);
row.createCell(1).setValue(COL1);
row.createCell(2).setValue(COL2);
row.createCell(3).setValue(COL3);
。
。
。
////////////////////////////////////////////////// ///////////////////////////// ////////////////////////////////////////////////// /////////////////////////////
//////现在,您可以用HSSFWorkbook对象时被发送到客户端//////
////////////////////////////////////////////////// ///////////////////////////// ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
hwb.write(BAOS);
excelStream =新ByteArrayInputStream进行(baos.toByteArray()); ////////////////////////////////////////////////// /////////////////////////////
////这里HSSFWorkbook对象时直接发送到客户端的W / O节省服务器///
////////////////////////////////////////////////// /////////////////////////////
}赶上(例外五){
的System.out.println(e.getMessage());
}
返回成功;
}
现在在struts-config文件只写(注意excelStream&放大器; contentDisposition已经在行动本身也是结果类型在这里被设置为 org.apache.struts2.dispatcher.StreamResult
)
<作用NAME =actionName级=actionClass>
<结果类型=流>
< PARAM NAME =的contentType>中的应用程序/ vnd.ms - Excel的< /参数>
< PARAM NAME =inputName> excelStream< /参数>
< PARAM NAME =contentDisposition> contentDisposition< /参数>
< PARAM NAME =缓冲区大小> 1024 LT; /参数>
< /&结果GT;
< /作用>
这就是它。现在,在执行操作时,用户将被提示保存或打开文件。
:)
As the title suggest, I need to put some data (which I have got from database) into an excel sheet, and then send it to client side so that user can save , open or cancel the action.
I have seen some articles regarding this, the closest one being : How can I get the user to download my file? (Java, MVC, Excel, POI). Referring to links provided by Stevens I tried out the following code :
public String execute(){
setContentDisposition("attachment; filename=\"" + ename + "\"");
try{
ServletContext servletContext = ServletActionContext.getServletContext();
String filePath = servletContext.getRealPath("/WEB-INF/template/excel/mytemplate.xls");
File file = new File(filePath);
Workbook wb = WorkbookFactory.create(new FileInputStream(file));
Sheet sheet = wb.getSheetAt(0);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
wb.write(baos);
InputStream excelStream;
excelStream = new ByteArrayInputStream(baos.toByteArray());
}catch(Exception e){
System.out.println(e.getMessage());
}
return SUCCESS;
}
Here firstly WorkbookFactory
is not defined. Secondly, I could not understand properly how the code is working.
I also found this link : http://www.roseindia.net/answers/viewqa/Java-Beginners/14930-How-to-export-data-from-database-to-excel-sheet-by-using-java--in-standalone-project.html. But here the excel file gets saved on the server. I want that the file should not be saved on the server side, it should directly go to client side
(If it helps) I am using : struts 2 framework, hibernate
I am open to using other things like POI API, jQuery or any other good stuff.
I can not use displayTag
for some reason.
Javascript would be my last resort (although I have implemented with it) because it requires changing some default security settings of the browser (If this can be avoided I am open to javascript as well).
Please advise how should I go about this now.
Thanks!!
EDIT :
<result-types>
<result-type name="jsp" class="org.apache.struts2.views.jsp"/>
<result-type name="tiles" class="org.apache.struts2.views.tiles.TilesResult"/>
<result-type name="stream" class="org.apache.struts2.dispatcher.StreamResult"/>
</result-types>
<action name="myActionName" class="package.myActionClass">
<result type="stream">
<param name="contentType">"application/vnd.ms-excel"</param>
<param name="inputName">excelStream</param>
<param name="contentDisposition">contentDisposition</param>
<param name="bufferSize">1024</param>
</result>
</action>
The error while executing action:
java.lang.reflect.InvocationTargetException
java.lang.IncompatibleClassChangeError: Class org.apache.poi.hssf.usermodel.HSSFWorkbook does not implement the requested interface org.apache.poi.ss.usermodel.Workbook
Okay. So finally I am through with all the roadblocks and have figured out a way to do this.
I realized that the problem I was facing was not in creating the excel file, the problem was sending it to client side, and that too without creating a file or temporary file on the server.
So here is how to go about it (I have ripped off details from my original code so that you can easily understand it).
In the action file you first have to create a HSSFWorkbook object, put data on it and then without saving it to disk on server, send it to client using inputstream.
Action File code :
public String execute(){
setContentDisposition("attachment; filename=\"" + ename + ".xls\"");
try{
HSSFWorkbook hwb=new HSSFWorkbook();
HSSFSheet sheet = hwb.createSheet("new sheet");
//////You can repeat this part using for or while to create multiple rows//////
HSSFRow row = sheet.createRow(rowNum);
row.createCell(0).setValue("col0");
row.createCell(1).setValue("col1");
row.createCell(2).setValue("col2");
row.createCell(3).setValue("col3");
.
.
.
///////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////
//////Now you are ready with the HSSFworkbook object to be sent to client//////
///////////////////////////////////////////////////////////////////////////////
ByteArrayOutputStream baos = new ByteArrayOutputStream();
hwb.write(baos);
excelStream = new ByteArrayInputStream(baos.toByteArray());
///////////////////////////////////////////////////////////////////////////////
////Here HSSFWorkbook object is sent directly to client w/o saving on server///
///////////////////////////////////////////////////////////////////////////////
}catch(Exception e){
System.out.println(e.getMessage());
}
return SUCCESS;
}
Now in the struts-config file just write (note that excelStream & contentDisposition has been set in the action itself also the result-type here is org.apache.struts2.dispatcher.StreamResult
):
<action name="actionName" class="actionClass">
<result type="stream">
<param name="contentType">"application/vnd.ms-excel"</param>
<param name="inputName">excelStream</param>
<param name="contentDisposition">contentDisposition</param>
<param name="bufferSize">1024</param>
</result>
</action>
Thats it. Now when the action is executed, the user will be prompted to save or open the file.
:)
这篇关于JAVA:导出数据(数据库),以擅长并将其发送给客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!