• 大小: 1.26MB
    文件类型: .rar
    金币: 1
    下载: 0 次
    发布日期: 2023-11-17
  • 语言: 其他
  • 标签: 源码  工具  

资源简介

导出数据库数据到Excel(源代码) 博文链接:https://hnylj.iteye.com/blog/165581

资源截图

代码片段和文件信息

package org.hnylj.poi;

import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.ResultSetmetaData;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class BuildExcelServlet extends HttpServlet {

protected void doGet(HttpServletRequest request
HttpServletResponse response) throws ServletException IOException {
StudentResultSet stuResultSet = new StudentResultSet();
ResultSet rs = stuResultSet.query();
String xlsName = “test.xls“;
String sheetName = “sheetName“;

HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
workbook.setSheetName(0 sheetName);
HSSFRow row = sheet.createRow((short) 0);
HSSFCell cell;
try {
ResultSetmetaData md = rs.getmetaData();
int nColumn = md.getColumnCount();

for (int i = 1; i <= nColumn; i++) {
cell = row.createCell((short) (i - 1));
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(new HSSFRichTextString(md.getColumnLabel(i)));
}
int iRow = 1;
while (rs.next()) {
row = sheet.createRow((short) iRow);
;
for (int j = 1; j <= nColumn; j++) {
cell = row.createCell((short) (j - 1));
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue(new HSSFRichTextString(rs.getobject(j)
.toString()));
}
iRow++;
}
String filename = request.getRealPath(“/“) + xlsName;
request.setAttribute(“filename“ filename);
FileOutputStream fOut = new FileOutputStream(filename);
workbook.write(fOut);
fOut.flush();
fOut.close();
request.getRequestDispatcher(“OpenExcelServlet“).forward(request
response);
} catch (Exception e) {
e.printStackTrace();
}
}

protected void doPost(HttpServletRequest request
HttpServletResponse response) throws ServletException IOException {
this.doGet(request response);
}
}

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件        505  2008-02-27 19:16  poi\.classpath

     目录          0  2008-02-27 15:38  poi\.myeclipse

     文件        282  2008-02-27 15:38  poi\.mymetadata

     文件       1221  2008-02-27 15:40  poi\.project

     文件       2324  2008-02-27 20:22  poi\src\org\hnylj\poi\BuildExcelServlet.java

     文件       1220  2008-02-27 20:38  poi\src\org\hnylj\poi\OpenExcelServlet.java

     文件        931  2008-02-27 21:21  poi\src\org\hnylj\poi\StudentResultSet.java

     文件       1834  2008-02-27 20:17  poi\src\org\hnylj\poi\Test.java

     目录          0  2008-02-27 20:17  poi\src\org\hnylj\poi

     目录          0  2008-02-27 15:59  poi\src\org\hnylj

     目录          0  2008-02-27 15:59  poi\src\org

     目录          0  2008-02-27 15:59  poi\src

     文件       6144  2008-02-27 19:16  poi\test.xls

     文件        361  2008-02-27 21:01  poi\WebRoot\index.jsp

     文件         39  2008-02-27 15:38  poi\WebRoot\meta-INF\MANIFEST.MF

     目录          0  2008-02-27 15:38  poi\WebRoot\meta-INF

     文件        696  2008-02-27 21:20  poi\WebRoot\sql.sql

     文件       3755  2008-02-27 20:22  poi\WebRoot\WEB-INF\classes\org\hnylj\poi\BuildExcelServlet.class

     文件       1830  2008-02-27 20:38  poi\WebRoot\WEB-INF\classes\org\hnylj\poi\OpenExcelServlet.class

     文件       1468  2008-02-27 21:21  poi\WebRoot\WEB-INF\classes\org\hnylj\poi\StudentResultSet.class

     文件       2774  2008-02-27 20:20  poi\WebRoot\WEB-INF\classes\org\hnylj\poi\Test.class

     目录          0  2008-02-27 20:17  poi\WebRoot\WEB-INF\classes\org\hnylj\poi

     目录          0  2008-02-27 19:16  poi\WebRoot\WEB-INF\classes\org\hnylj

     目录          0  2008-02-27 19:16  poi\WebRoot\WEB-INF\classes\org

     目录          0  2008-02-27 19:16  poi\WebRoot\WEB-INF\classes

     文件     493105  2008-02-27 19:16  poi\WebRoot\WEB-INF\lib\mysql-connector-java-5.0.3-bin.jar

     文件     909270  2008-02-27 15:38  poi\WebRoot\WEB-INF\lib\poi-3.0.1.jar

     目录          0  2008-02-27 19:16  poi\WebRoot\WEB-INF\lib

     文件        932  2008-02-27 21:00  poi\WebRoot\WEB-INF\web.xml

     目录          0  2008-02-27 15:38  poi\WebRoot\WEB-INF

............此处省略5个文件信息

评论

共有 条评论