package servlet;
import java.io.IOException;
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.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.Region;
public class TestServlet extends HttpServlet {
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
// PrintWriter out = response.getWriter();
HSSFWorkbook wb=new HSSFWorkbook();
// FileOutputStream fileOut=new FileOutputStream("c:\\a.xls");
//创建一个文件
HSSFSheet sheet=wb.createSheet("new sheet");
//创建一个工作区
HSSFRow row =sheet.createRow((short)0);
//创建一行
HSSFCell cell=row.createCell((short)0);
// HSSFCell cell1=row.createCell((short)4);
cell.setCellValue(1);
row.createCell((short)1).setCellValue(1.2);
HSSFCell cell2=row.createCell((short)2);
cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
//改变文字编码
cell2.setCellValue("你好");
HSSFRow row2=sheet.createRow(2);
HSSFCell cell20=row2.createCell((short)0);
cell20.setCellValue(true);
HSSFSheet sheet2=wb.createSheet("new sheet1");
HSSFRow rowx=sheet2.createRow(0);
HSSFCell cellx=rowx.createCell((short)0);
cellx.setCellValue("aa");
sheet2.addMergedRegion(new Region(0,(short)0,0,(short)2));
sheet2.addMergedRegion(new Region(3,(short)0,5,(short)8));
HSSFRow rowxx=sheet2.createRow(3);
HSSFCell cellxx=rowxx.createCell((short)0);
cellxx.setCellValue("xxxxx");
HSSFSheet sheet3=wb.createSheet("new sheet2");
sheet3.createFreezePane(1,0);
//冻结1列 冻结2行
// sheet3.createFreezePane(3,1,4,2);
HSSFRow rowxxx=sheet3.createRow(0);
cteatecell(wb,rowxxx,(short)0,HSSFCellStyle.ALIGN_CENTER,"aa");
cteatecell(wb,rowxxx,(short)1,HSSFCellStyle.ALIGN_CENTER,"bb");
cteatecell(wb,rowxxx,(short)2,HSSFCellStyle.ALIGN_LEFT,"cc");
cteatecell(wb,rowxxx,(short)3,HSSFCellStyle.ALIGN_RIGHT,"dd");
request.setAttribute("wb",wb);
request.getRequestDispatcher("../download.jsp").forward(request,response);
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occure
*/
public void init() throws ServletException {
// Put your code here
}
private static void cteatecell(HSSFWorkbook wb,HSSFRow row,short col,short align,String val)
{
HSSFCell cell=row.createCell(col);
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue(val);
HSSFCellStyle cellstyle=wb.createCellStyle();
cellstyle.setAlignment(align);
cell.setCellStyle(cellstyle);
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
Apache poi教程和例子
共9个文件
mht:3个
java:3个
doc:2个
4星 · 超过85%的资源 需积分: 50 129 下载量 34 浏览量
2009-06-20
09:54:21
上传
评论
收藏 403KB RAR 举报
温馨提示
在我们实际的开发中,表现层的解决方案虽然有多样,但是IE浏览器已成为最多人使用的浏览器,因为大家都用Windows。在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统、银行系统)。或者是:我们已经习惯用Excel打印。 Apache的Jakata项目的POI子项目,目前比较成熟的是HSSF接口,处理MSExcel对象。它不象我们仅仅是用csv生成的没有格式的可以由Excel转换的东西,而是真正的Excel对象,你可以控制一些属性如sheet,cell等等。
资源推荐
资源详情
资源评论
收起资源包目录
poi教程.rar (9个子文件)
poi教程
excel实时生成
CSDN技术中心 文档列表.mht 94KB
CSDN技术中心 Struts实时生成Excel文件下载.mht 69KB
TestServlet.java 4KB
CSDN技术中心 Struts生成Word下载.mht 72KB
download.jsp 481B
AA.java 2KB
FixationExcel.java 4KB
poi教程(ok).doc 351KB
poi教程(ok).doc 351KB
共 9 条
- 1
tony_4539
- 粉丝: 1
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
前往页