如何将数据从 struts2 导出到 Excel
How to export data from struts2 to Excel
我希望数据以 Excel 格式导出。我在将数据写入 Excel 文件时遇到问题,当我下载时它不是 Excel 格式。
在下面的第一种方法中,我从数据库中获取数据并将其作为列表发送到需要文件名的第二种方法。在第二种方法中,我试图将数据库数据写入 Excel 文件。
我也完成了导出为 PDF 的编码。它已正确下载,但在 URL 栏中 downloadTeacherListINExcel.action
。我给出了这样的文件路径:String fileName = "f:\teachersList.pdf"
。在浏览器中下载时,文件名会变成 f-teachersList
.
public String exportInExcel() {
// getting data from data base
listOfTeachers = reportService.getlistOfTeachers();
for (TeacherDTO teacherDTO : listOfTeachers) {
System.out.println(teacherDTO.getTeacherEmailID());
}
// sending a list data export in excel
String excelFileName = reportService.exportInExcel(listOfTeachers);
System.out.println(excelFileName);
try {
fileInputStream = new FileInputStream(excelFileName);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Execl Download Method");
return SUCCESS;
}
为Excel
编写的代码
@Override
public String exportInExcel(List<TeacherDTO> listOfTeachers) {
String fileName = "f:\test\teachersList.xls";
try {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Products List");
// create heading
Row rowHeading = sheet.createRow(0);
rowHeading.createCell(0).setCellValue("Id");
rowHeading.createCell(1).setCellValue("Name");
for (int i = 0; i < 6; i++) {
CellStyle stylerowHeading = workbook.createCellStyle();
Font font = workbook.createFont();
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
font.setFontName(HSSFFont.FONT_ARIAL);
font.setFontHeightInPoints((short) 11);
stylerowHeading.setFont(font);
stylerowHeading.setVerticalAlignment(CellStyle.ALIGN_CENTER);
rowHeading.getCell(i).setCellStyle(stylerowHeading);
}
int r = 1;
for (TeacherDTO teacher : listOfTeachers) {
Row row = sheet.createRow(r);
// Id column
Cell cellID = row.createCell(0);
cellID.setCellValue(teacher.getTeacherFirstName());// (run after
// this line
// once)
// name column
Cell cellName = row.createCell(1);
cellName.setCellValue(teacher.getTeacherEmailID());// (run after
// this line
// once)
r++;
}
// Auto fit
for (int i = 0; i < 6; i++) {
sheet.autoSizeColumn(i);
}
FileOutputStream fout = new FileOutputStream(new File(fileName));
workbook.write(fout);
fout.close();
System.out.println("Excel Written Success");
} catch (Exception e) {
System.out.println("%%%%%%%%%%%%%%%%%%"+e.getMessage());
}
return fileName;
}
Struts 配置
<action name="downloadTeacherListINExcel" class="com.pradeep.sms.controller.report.StaffReportAction" method="exportInExcel">
<result name="success" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment;filename=${fileName}</param>
<param name="bufferSize">1024</param>
</result>
</action>
错误
Hibernate: select
pradeepkumarhe1989@gmail.com
%%%%%%%%%%%%%%%%%%null
f:\test\teachersList.xls
java.io.FileNotFoundException: f:\test\teachersList.xls (The system cannot find the file specified)
正在行动class
public String exportInExcel() {
//getting List of teachers
listOfTeachers = reportServiceExcel.getlistOfTeachers();
// sending list data to write in excel sheet
HSSFWorkbook workbook = reportServiceExcel.exportInExcel(listOfTeachers);
// code to download
try {
ByteArrayOutputStream boas = new ByteArrayOutputStream();
workbook.write(boas);
setInputStream(new ByteArrayInputStream(boas.toByteArray()));
} catch (IOException e) {
e.printStackTrace();
}
return SUCCESS;
}
Excel class
public HSSFWorkbook exportInExcel(List<TeacherDTO> listOfTeachers) {
HSSFWorkbook workbook = null;
try {
workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Products List");
// create heading
Row rowHeading = sheet.createRow(0);
rowHeading.createCell(0).setCellValue("Name");
rowHeading.createCell(1).setCellValue("Mobile Number");
rowHeading.createCell(2).setCellValue("Email ID");
rowHeading.createCell(3).setCellValue("Designation");
for (int i = 0; i < 4; i++) {
CellStyle stylerowHeading = workbook.createCellStyle();
Font font = workbook.createFont();
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
font.setFontName(HSSFFont.FONT_ARIAL);
font.setFontHeightInPoints((short) 11);
stylerowHeading.setFont(font);
stylerowHeading.setVerticalAlignment(CellStyle.ALIGN_CENTER);
rowHeading.getCell(i).setCellStyle(stylerowHeading);
}
int r = 1;
for (TeacherDTO t : listOfTeachers) {
String teacherName = t.getTeacherFirstName() + "" + t.getTeacherMiddleName() + ""
+ t.getTeacherLastName();
Row row = sheet.createRow(r);
// Name column
Cell cellName = row.createCell(0);
cellName.setCellValue(teacherName);// (run after this line once)
// Mobile Number column
Cell cellMobileNumber = row.createCell(1);
cellMobileNumber.setCellValue(t.getTeacherMobileNumber());
// Email column
Cell cellEmail = row.createCell(2);
cellEmail.setCellValue(t.getTeacherEmailID());
// Designation column
Cell cellDesignation = row.createCell(3);
cellDesignation.setCellValue(t.getTeacherDesignation());
r++;
}
// Auto fit columns in excel sheet
for (int i = 0; i < 4; i++) {
sheet.autoSizeColumn(i);
}
System.out.println("Excel Written Success");
} catch (Exception e) {
e.printStackTrace();
}
return workbook;
}
Struts 配置
<action name="downloadTeacherListExcel" class="com.pradeep.sms.controller.report.StaffReportAction" method="exportInExcel">
<result name="success" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="teachersList.xls"</param>
<param name="bufferSize">4096</param>
</result>
</action>
com.download2excel 包;
进口java.io.ByteArrayInputStream;导入 java.io.ByteArrayOutputStream;导入 java.io.File;导入 java.io.FileInputStream;导入 java.io.FileOutputStream;导入 java.io.InputStream;导入 org.apache.poi.hssf.usermodel.HSSFRow;导入 org.apache.poi.hssf.usermodel.HSSFSheet;导入 org.apache.poi.hssf.usermodel.HSSFWorkbook;导入 com.opensymphony.xwork2.ActionSupport;
public class Excelwritinginjava extends ActionSupport { private InputStream fileInputStream;
public String createExcelFile() throws Exception { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("January"); HSSFRow rowhead = sheet.createRow((short) 0); rowhead.createCell(0).setCellValue("S.No."); rowhead.createCell(1).setCellValue("Customer Name"); rowhead.createCell(2).setCellValue("Account Number"); rowhead.createCell(3).setCellValue("e-mail"); rowhead.createCell(4).setCellValue("Balance"); HSSFRow row = sheet.createRow((short) 1); row.createCell(0).setCellValue("1"); row.createCell(1).setCellValue("subodh"); row.createCell(2).setCellValue("7644864977"); row.createCell(3).setCellValue("subodh.orcl@gmail.com"); row.createCell(4).setCellValue("700000.00"); HSSFRow row1 = sheet.createRow((short) 2); row1.createCell(0).setCellValue("2"); row1.createCell(1).setCellValue("subodh sharma"); row1.createCell(2).setCellValue("22222222"); row1.createCell(3).setCellValue("subodh.orcl@gmail.com"); row1.createCell(4).setCellValue("200000.00"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); workbook.write(baos); setFileInputStream(new ByteArrayInputStream(baos.toByteArray())); return SUCCESS; }
public InputStream getFileInputStream() { return fileInputStream; }
public void setFileInputStream(InputStream fileInputStream) { this.fileInputStream = fileInputStream; }
}
======================struts.xml ====
application/octet-stream fileInputStream 附件;文件名="Download.xls" 1024
我希望数据以 Excel 格式导出。我在将数据写入 Excel 文件时遇到问题,当我下载时它不是 Excel 格式。
在下面的第一种方法中,我从数据库中获取数据并将其作为列表发送到需要文件名的第二种方法。在第二种方法中,我试图将数据库数据写入 Excel 文件。
我也完成了导出为 PDF 的编码。它已正确下载,但在 URL 栏中 downloadTeacherListINExcel.action
。我给出了这样的文件路径:String fileName = "f:\teachersList.pdf"
。在浏览器中下载时,文件名会变成 f-teachersList
.
public String exportInExcel() {
// getting data from data base
listOfTeachers = reportService.getlistOfTeachers();
for (TeacherDTO teacherDTO : listOfTeachers) {
System.out.println(teacherDTO.getTeacherEmailID());
}
// sending a list data export in excel
String excelFileName = reportService.exportInExcel(listOfTeachers);
System.out.println(excelFileName);
try {
fileInputStream = new FileInputStream(excelFileName);
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("Execl Download Method");
return SUCCESS;
}
为Excel
编写的代码@Override
public String exportInExcel(List<TeacherDTO> listOfTeachers) {
String fileName = "f:\test\teachersList.xls";
try {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Products List");
// create heading
Row rowHeading = sheet.createRow(0);
rowHeading.createCell(0).setCellValue("Id");
rowHeading.createCell(1).setCellValue("Name");
for (int i = 0; i < 6; i++) {
CellStyle stylerowHeading = workbook.createCellStyle();
Font font = workbook.createFont();
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
font.setFontName(HSSFFont.FONT_ARIAL);
font.setFontHeightInPoints((short) 11);
stylerowHeading.setFont(font);
stylerowHeading.setVerticalAlignment(CellStyle.ALIGN_CENTER);
rowHeading.getCell(i).setCellStyle(stylerowHeading);
}
int r = 1;
for (TeacherDTO teacher : listOfTeachers) {
Row row = sheet.createRow(r);
// Id column
Cell cellID = row.createCell(0);
cellID.setCellValue(teacher.getTeacherFirstName());// (run after
// this line
// once)
// name column
Cell cellName = row.createCell(1);
cellName.setCellValue(teacher.getTeacherEmailID());// (run after
// this line
// once)
r++;
}
// Auto fit
for (int i = 0; i < 6; i++) {
sheet.autoSizeColumn(i);
}
FileOutputStream fout = new FileOutputStream(new File(fileName));
workbook.write(fout);
fout.close();
System.out.println("Excel Written Success");
} catch (Exception e) {
System.out.println("%%%%%%%%%%%%%%%%%%"+e.getMessage());
}
return fileName;
}
Struts 配置
<action name="downloadTeacherListINExcel" class="com.pradeep.sms.controller.report.StaffReportAction" method="exportInExcel">
<result name="success" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="inputName">fileInputStream</param>
<param name="contentDisposition">attachment;filename=${fileName}</param>
<param name="bufferSize">1024</param>
</result>
</action>
错误
Hibernate: select
pradeepkumarhe1989@gmail.com
%%%%%%%%%%%%%%%%%%null
f:\test\teachersList.xls
java.io.FileNotFoundException: f:\test\teachersList.xls (The system cannot find the file specified)
正在行动class
public String exportInExcel() {
//getting List of teachers
listOfTeachers = reportServiceExcel.getlistOfTeachers();
// sending list data to write in excel sheet
HSSFWorkbook workbook = reportServiceExcel.exportInExcel(listOfTeachers);
// code to download
try {
ByteArrayOutputStream boas = new ByteArrayOutputStream();
workbook.write(boas);
setInputStream(new ByteArrayInputStream(boas.toByteArray()));
} catch (IOException e) {
e.printStackTrace();
}
return SUCCESS;
}
Excel class
public HSSFWorkbook exportInExcel(List<TeacherDTO> listOfTeachers) {
HSSFWorkbook workbook = null;
try {
workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Products List");
// create heading
Row rowHeading = sheet.createRow(0);
rowHeading.createCell(0).setCellValue("Name");
rowHeading.createCell(1).setCellValue("Mobile Number");
rowHeading.createCell(2).setCellValue("Email ID");
rowHeading.createCell(3).setCellValue("Designation");
for (int i = 0; i < 4; i++) {
CellStyle stylerowHeading = workbook.createCellStyle();
Font font = workbook.createFont();
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
font.setFontName(HSSFFont.FONT_ARIAL);
font.setFontHeightInPoints((short) 11);
stylerowHeading.setFont(font);
stylerowHeading.setVerticalAlignment(CellStyle.ALIGN_CENTER);
rowHeading.getCell(i).setCellStyle(stylerowHeading);
}
int r = 1;
for (TeacherDTO t : listOfTeachers) {
String teacherName = t.getTeacherFirstName() + "" + t.getTeacherMiddleName() + ""
+ t.getTeacherLastName();
Row row = sheet.createRow(r);
// Name column
Cell cellName = row.createCell(0);
cellName.setCellValue(teacherName);// (run after this line once)
// Mobile Number column
Cell cellMobileNumber = row.createCell(1);
cellMobileNumber.setCellValue(t.getTeacherMobileNumber());
// Email column
Cell cellEmail = row.createCell(2);
cellEmail.setCellValue(t.getTeacherEmailID());
// Designation column
Cell cellDesignation = row.createCell(3);
cellDesignation.setCellValue(t.getTeacherDesignation());
r++;
}
// Auto fit columns in excel sheet
for (int i = 0; i < 4; i++) {
sheet.autoSizeColumn(i);
}
System.out.println("Excel Written Success");
} catch (Exception e) {
e.printStackTrace();
}
return workbook;
}
Struts 配置
<action name="downloadTeacherListExcel" class="com.pradeep.sms.controller.report.StaffReportAction" method="exportInExcel">
<result name="success" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename="teachersList.xls"</param>
<param name="bufferSize">4096</param>
</result>
</action>
com.download2excel 包;
进口java.io.ByteArrayInputStream;导入 java.io.ByteArrayOutputStream;导入 java.io.File;导入 java.io.FileInputStream;导入 java.io.FileOutputStream;导入 java.io.InputStream;导入 org.apache.poi.hssf.usermodel.HSSFRow;导入 org.apache.poi.hssf.usermodel.HSSFSheet;导入 org.apache.poi.hssf.usermodel.HSSFWorkbook;导入 com.opensymphony.xwork2.ActionSupport;
public class Excelwritinginjava extends ActionSupport { private InputStream fileInputStream;
public String createExcelFile() throws Exception { HSSFWorkbook workbook = new HSSFWorkbook(); HSSFSheet sheet = workbook.createSheet("January"); HSSFRow rowhead = sheet.createRow((short) 0); rowhead.createCell(0).setCellValue("S.No."); rowhead.createCell(1).setCellValue("Customer Name"); rowhead.createCell(2).setCellValue("Account Number"); rowhead.createCell(3).setCellValue("e-mail"); rowhead.createCell(4).setCellValue("Balance"); HSSFRow row = sheet.createRow((short) 1); row.createCell(0).setCellValue("1"); row.createCell(1).setCellValue("subodh"); row.createCell(2).setCellValue("7644864977"); row.createCell(3).setCellValue("subodh.orcl@gmail.com"); row.createCell(4).setCellValue("700000.00"); HSSFRow row1 = sheet.createRow((short) 2); row1.createCell(0).setCellValue("2"); row1.createCell(1).setCellValue("subodh sharma"); row1.createCell(2).setCellValue("22222222"); row1.createCell(3).setCellValue("subodh.orcl@gmail.com"); row1.createCell(4).setCellValue("200000.00"); ByteArrayOutputStream baos = new ByteArrayOutputStream(); workbook.write(baos); setFileInputStream(new ByteArrayInputStream(baos.toByteArray())); return SUCCESS; }
public InputStream getFileInputStream() { return fileInputStream; }
public void setFileInputStream(InputStream fileInputStream) { this.fileInputStream = fileInputStream; }
}
======================struts.xml ====
application/octet-stream fileInputStream 附件;文件名="Download.xls" 1024