本文目录一览:
- 1、java poi导出excel要双击才显示换行?
- 2、poi读取Excel,之后生成另一个Excel
- 3、POI生成excel问题
- 4、java poi 在服务器生成excel文件
- 5、poi将数据生成excel中,流程是怎么实现的?
- 6、javaweb中通过POI生成Excel并弹出下载窗口!
java poi导出excel要双击才显示换行?
在开始选项卡下面有个玩意叫自动换行,点一下就好了。
如果找不到,全选表格,右击,设置单元格格式,对齐,勾选自动换行即可。
poi读取Excel,之后生成另一个Excel
File fi=new File("D://20110221144419.xls");
System.out.println(11);
//创建一个工作薄
POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream(fi));
HSSFWorkbook wb=new HSSFWorkbook(fs);
int i = 0;
HSSFSheet hsheet = wb.getSheetAt(i);
if(hsheet==null)
hsheet = wb.createSheet("Sheet"+(i)+"");
HSSFPatriarch patriarch = hsheet.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,512,255,(short) 1,1,(short)10,20);
patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
fileOut= new FileOutputStream(fi);
//写入excel文件
wb.write(fileOut);
fileOut.close();
POI生成excel问题
XSSF不能读取Excel2003以前(包括2003)的版本,
没需要就按你之前的继续,如果在读取前判断文件是2003前的版本还是2007的版本,提供个思路。XSSF和HSSF虽然在不同的包里,但却引用了同一接口Workbook,
Workbook book = null;
try {
book = new XSSFWorkbook(excelFile);
} catch (Exception ex) {
book = new HSSFWorkbook(new FileInputStream(excelFile));
}
各版本的Excel中测试,没有发生异常
java poi 在服务器生成excel文件
报格式错误是因为你没有填充EXCEL的内容。
正确的做法是:
1, HSSFWorkbook ws = new HSSFWorkbook();//建立新HSSFWorkbook对象
2, Sheet sheet = workbook.createSheet(0); //建立一个新的sheet
3,Row row = sheet.createRow(1); //建立一个新的row对象
4, Cell cell = row.createCell(0); //在row上创建方格即列,
cell.setCellValue(cellValue); //设置这个行中列的值
cell.setCellStyle(cellStyle); //设置样式
poi将数据生成excel中,流程是怎么实现的?
/**
* 请求处理
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
public void myself(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//定义文件的绝对路径
String realpath=request.getSession().getServletContext().getRealPath("");
System.out.println("realpath--"+realpath);
//获得跳转页面的路径
String url=request.getParameter("url");
System.out.println("url--"+url);
String filepath=ExcelMaker.makeExcel(excelModelPath,realpath);
System.out.println("newfilepath--"+filepath);
if(url!=nullurl.length()0){
request.setAttribute("filepath", filepath);
request.getRequestDispatcher(url).forward(request, response);
}
}
/**
* 生成excel文件
* @author 尹义恒
*
*/
public class ExcelMaker {
private static String filepath="";
private static String newfilepath="";
//通过excel模板生成
public static String makeExcel(String modelpath,String realpath){
try {
//选择模板文件
Workbook wb=Workbook.getWorkbook(new File(realpath+"/yyh/"+modelpath));
//创建一个新的文件
newfilepath="/yyh/yyh.xls";
filepath=realpath+newfilepath;
File targetFile=new File(filepath);
targetFile.createNewFile();
//给新的文件加载模板
WritableWorkbook wwb=Workbook.createWorkbook(targetFile, wb);
ExcelMgr.makeExcel(wwb,realpath);
wwb.write();
wwb.close();
wb.close();
} catch (Exception e) {
}
return newfilepath;
}
//不通过模板生成
public static String makeExcel(String realpath){
try {
filepath=realpath+"/yyh.xls";
WritableWorkbook wwb=Workbook.createWorkbook(new File(filepath));
ExcelMgr.makeExcel(wwb,realpath);
} catch (IOException e) {
e.printStackTrace();
}
return filepath;
}
}
/**
* excel文件的管理
* @author 尹义恒
*
*/
public class ExcelMgr {
//定义一个存储数据的集合
private static ListYyhMyself list=new ArrayListYyhMyself();
//定义一个业务层
private static YyhMyselfService service=new YyhMyselfService();
private ListYyhMyself getList() {
return list;
}
public void setList(ListYyhMyself list) {
this.list = list;
}
//生成excel文件
public static void makeExcel(WritableWorkbook wwb,String realpath){
init();
WritableSheet sheet=wwb.getSheet(0);
//加载样式
FormatStyle.init();
fillCell(sheet,realpath);
}
//加载数据
public static void init(){
//给集合加载数据
list=service.findAll();
}
//填充单元格
public static void fillCell(WritableSheet sheet,String realpath){
for(int i=0;ilist.size();i++){
YyhMyself yyh=list.get(i);
int co=yyh.getCo();
int ro=yyh.getRo();
String value=yyh.getVal();
String type=yyh.getType();
try {
if(type.equals("string")){
System.out.println("string--"+value);
Label cell=new Label( co,ro,value,FormatStyle.detFormat);
sheet.addCell(cell);
}else if(type.equals("int")){
System.out.println("int--"+value);
int val=new Integer(value);
jxl.write.Number cell=new jxl.write.Number(co,ro,val,FormatStyle.int_format);
sheet.addCell(cell);
}else if(type.equals("times")){
System.out.println("date--"+value);
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date date=sdf.parse(value);
DateTime cell=new DateTime(co, ro, date, FormatStyle.date_format);
sheet.addCell(cell);
}else if(type.equals("img")){
System.out.println("img--"+value);
System.out.println("realpath--"+realpath);
File image=new File(realpath+"/"+value);
WritableImage img=new WritableImage(co, ro, 2, 2, image);
sheet.addImage(img);
}else if(type.equals("double")){
System.out.println("double--"+value);
double val=new Double(value);
jxl.write.Number cell=new jxl.write.Number(co,ro,val,FormatStyle.double_format);
sheet.addCell(cell);
}else if(type.equals("float")){
System.out.println("float--"+value);
float val=new Float(value);
jxl.write.Number cell= new jxl.write.Number(co,ro,val,FormatStyle.float_format);
sheet.addCell(cell);
}else{
System.out.println("没有查到对应的数据!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
基本流程就是这样
javaweb中通过POI生成Excel并弹出下载窗口!
把response的输出类型设置成
response.setContentType("application/x-download
response.addHeader("Content-Disposition","attachment;filename=myexcel.xls" );
POI结果直接给response的输出流,就可以了