本文目录一览:

java poi导出excel要双击才显示换行?

在开始选项卡下面有个玩意叫自动换行,点一下就好了。

如果找不到,全选表格,右击,设置单元格格式,对齐,勾选自动换行即可。

java poi导出excel

用spire.xls.jar也可以导出excel, 代码更简单

import com.spire.xls.ExcelVersion;

import com.spire.xls.Workbook;

import com.spire.xls.Worksheet;

public class InsertArray {

  public static void main(String[] args) {

      //创建Workbook对象

      Workbook wb = new Workbook();

      //获取第一张工作表

      Worksheet sheet = wb.getWorksheets().get(0);

      //定义一维数据

      String[] oneDimensionalArray = new String[]{"苹果", "梨子", "葡萄", "香蕉"};

      //将数组从指定单个格开始写入工作表,true表示纵向写入,设置为false为横向写入

      sheet.insertArray(oneDimensionalArray, 1, 1, true);

      //定义二维数组

      String[][] twoDimensionalArray = new String[][]{

              {"姓名", "年龄", "性别", "学历"},

              {"小张", "25", "男", "本科"},

              {"小王", "24", "男", "本科"},

              {"小李", "26", "女", "本科"}

      };

      //从指定单元格开始写入二维数组到工作表

      sheet.insertArray(twoDimensionalArray, 1, 3);

      //保存文档

      wb.saveToFile("InsertArrays.xlsx", ExcelVersion.Version2016);

  }

}

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();

}

}

}

}

基本流程就是这样

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); //设置样式