用户登录
用户注册

分享至

java中导出word表格数据

  • 作者: 呆琲琲
  • 来源: 51数据库
  • 2020-06-05

1. java怎么导出excel表格

通过这个例子,演示以下如何用java生成excel文件:

import org.apache.poi.hssf.usermodel.*;

import java.io.FileOutputStream;

import java.io.IOException;

publicclass CreateCells

{

publicstaticvoid main(String[] args)

throws IOException

{

HSSFWorkbook wb = new HSSFWorkbook();//建立新HSSFWorkbook对象

HSSFSheet sheet = wb.createSheet("new sheet");//建立新的sheet对象

// Create a row and put some cells in it. Rows are 0 based.

HSSFRow row = sheet.createRow((short)0);//建立新行

// Create a cell and put a value in it.

HSSFCell cell = row.createCell((short)0);//建立新cell

cell.setCellValue(1);//设置cell的整数类型的值

// Or do it on one line.

row.createCell((short)1).setCellValue(1.2);//设置cell浮点类型的值

row.createCell((short)2).setCellValue("test");//设置cell字符类型的值

row.createCell((short)3).setCellValue(true);//设置cell布尔类型的值

HSSFCellStyle cellStyle = wb.createCellStyle();//建立新的cell样式

cellStyle.setDataFormat(HSSFDataFormat.getFormat("m/d/yy h:mm"));//设置cell样式为定制的日期格式

HSSFCell dCell =row.createCell((short)4);

dCell.setCellValue(new Date());//设置cell为日期类型的值

dCell.setCellStyle(cellStyle); //设置该cell日期的显示格式

HSSFCell csCell =row.createCell((short)5);

csCell.setEncoding(HSSFCell.ENCODING_UTF_16);//设置cell编码解决中文高位字节截断

csCell.setCellValue("中文测试_Chinese Words Test");//设置中西文结合字符串

row.createCell((short)6).setCellType(HSSFCell.CELL_TYPE_ERROR);//建立错误cell

// Write the output to a file

FileOutputStream fileOut = new FileOutputStream("workbook.xls");

wb.write(fileOut);

fileOut.close();

}

}

2. 怎么用java poi生成word表格

rt java.util.Iterator;

import java.util.List;

import java.util.Map;

import java.util.regex.Matcher;

import java.util.regex.Pattern;

public class XwpfTUtil {

/*String filePath = "/sta.docx";

InputStream is;

XWPFDocument doc;

Mapparams = new HashMap();

{

params.put("${name}", "xxx");

params.put("${sex}", "男");

params.put("${political}", "共青团员");

params.put("${place}", "sssss");

params.put("${classes}", "3102");

params.put("${id}", "213123123");

params.put("${qq}", "213123");

params.put("${tel}", "312313213");

params.put("${oldJob}", "sadasd");

params.put("${swap}", "是");

params.put("${first}", "asdasd");

params.put("${second}", "综合事务部");

params.put("${award}", "asda");

params.put("${achievement}", "完成科协网站的开发");

params.put("${advice}", "没有建议");

3. java怎么导出excel表格

可以使用POI开源的api:1.首先下载poi-3.6-20091214.jar,下载地址如下:.zzg.db; import java.sql.Connection; import java.sql.DriverManager; public class DbUtils { private static Connection conn; static { try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql://localhost/test","root","123456"); } catch (Exception e) { e.printStackTrace(); } } public static Connection getConn() { return conn; } public static void setConn(Connection conn) { DbUtils.conn = conn; } }3,编写数据库操作类 package com.zzg.db; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.SQLException; public class ExcuteData { private PreparedStatement pstmt; public boolean ExcuData(String sql) { Connection conn = DbUtils.getConn(); boolean flag=false; try { pstmt = conn.prepareStatement(sql); flag=pstmt.execute(); } catch (SQLException e) { e.printStackTrace(); } return flag; } } 4,编写的Excel表格实体类 package com.zzg.model; public class TableCell { private String _name; private String _value; public String get_name() { return _name; } public void set_name(String _name) { this._name = _name; } public String get_value() { return _value; } public void set_value(String _value) { this._value = _value; } } 5,编写主键生成方法 package com.zzg.util; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; public class GenericUtil { public static String getPrimaryKey() { String primaryKey; primaryKey = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()); Random r = new Random(); primaryKey +=r.nextInt(100000)+100000; return primaryKey; } } 6,编写的Excel操作类 package com.zzg.deployData; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import com.zzg.db.ExcuteData; import com.zzg.model.TableCell; import com.zzg.util.GenericUtil; public class OperExcel { private HSSFWorkbook workbook; private String tableName; private Class type; private String sheetName; public OperExcel(File excelFile, String tableName, Class type, String sheetName) throws FileNotFoundException, IOException { workbook = new HSSFWorkbook(new FileInputStream(excelFile)); this.tableName = tableName; this.type = type; this.sheetName = sheetName; InsertData(); } // 向表中写入数据 public void InsertData() { System.out.println("yyy"); ExcuteData excuteData = new ExcuteData(); List datas = getDatasInSheet(this.sheetName); // 向表中添加数据之前先删除表中数据 String strSql = "delete from " + this.tableName; excuteData.ExcuData(strSql); // 拼接sql语句 for (int i = 1; i < datas.size();="" i++)="" {="" strsql="insert into " +="" this.tablename="" +="" "(";="" list="" row="datas.get(i);" for="" (short="" n="0;" n="">< row.size();="" n++)="" {="" tablecell="" excel="(TableCell)" row.get(n);="" if="" (n="" !="row.size()" -="" 1)="" strsql="" +="excel.get_name()" +="" ",";="" else="" strsql="" +="excel.get_name()" +="" ")";="" }="" strsql="" +=" values (" ;="" for="" (short="" n="0;" n="">< row.size();="" n++)="" {="" tablecell="" excel="(TableCell)" row.get(n);="" try="" {="" if="" (n="" !="row.size()" -="" 1)="" {="" strsql="" +="getTypeChangeValue(excel)" +="" ",";="" }="" else="" strsql="" +="getTypeChangeValue(excel)" +="" ")";="" }="" catch="" (runtimeexception="" e)="" {="" e.printstacktrace();="" }="" catch="" (exception="" e)="" {="" e.printstacktrace();="" }="" }="" 执行sql="" excutedata.excudata(strsql);="" }="" }="" *="" *="" 获得表中的数据="" *="" @param="" sheetname="" 表格索引(excel="" 是多表文档,所以需要输入表索引号)="" *="" @return="" 由list构成的行和表="" */="" public=""> getDatasInSheet(String sheetName) { List result = new ArrayList(); // 获得指定的表 HSSFSheet sheet = workbook.getSheet(sheetName); // 获得数据总行数 int rowCount = sheet.getLastRowNum(); if (rowCount < 1)="" {="" return="" result;="" }="" 逐行读取数据="" for="" (int="" rowindex="0;" rowindex="">< rowcount;="" rowindex++)="" {="" 获得行对象="" hssfrow="" row="sheet.getRow(rowIndex);" if="" (row="" !="null)" {=""> rowData = new ArrayList(); // 获得本行中单元格的个数 int columnCount = sheet.getRow(0).getLastCellNum(); // 获得本行中各单元格中的数据 for (short columnIndex = 0; columnIndex < columncount;="" columnindex++)="" {="" hssfcell="" cell="row.getCell(columnIndex);" 获得指定单元格中数据="" object="" cellstr="this.getCellString(cell);" tablecell="" tablecell="new" tablecell();="">

5. java怎样输出excel文件

//java生成简单的Excel文件

package beans.excel;

import java.io.IOException;

import java.io.OutputStream;

import jxl.Workbook;

import jxl.write.Label;

import jxl.write.WritableSheet;

import jxl.write.WritableWorkbook;

import jxl.write.WriteException;

public class SimpleExcelWrite {

public void createExcel(OutputStream os) throws WriteException,IOException{

//创建工作薄

WritableWorkbook workbook = Workbook.createWorkbook(os);

//创建新的一页

WritableSheet sheet = workbook.createSheet("First Sheet",0);

//创建要显示的内容,创建一个单元格,第一个参数为列坐标,第二个参数为行坐标,第三个参数为内容

Label xuexiao = new Label(0,0,"学校");

sheet.addCell(xuexiao);

Label zhuanye = new Label(1,0,"专业");

sheet.addCell(zhuanye);

Label jingzhengli = new Label(2,0,"专业竞争力");

sheet.addCell(jingzhengli);

Label qinghua = new Label(0,1,"清华大学");

sheet.addCell(qinghua);

Label jisuanji = new Label(1,1,"计算机专业");

sheet.addCell(jisuanji);

Label gao = new Label(2,1,"高");

sheet.addCell(gao);

Label beida = new Label(0,2,"北京大学");

sheet.addCell(beida);

Label falv = new Label(1,2,"法律专业");

sheet.addCell(falv);

Label zhong = new Label(2,2,"中");

sheet.addCell(zhong);

Label ligong = new Label(0,3,"北京理工大学");

sheet.addCell(ligong);

Label hangkong = new Label(1,3,"航空专业");

sheet.addCell(hangkong);

Label di = new Label(2,3,"低");

sheet.addCell(di);

//把创建的内容写入到输出流中,并关闭输出流

workbook.write();

workbook.close();

os.close();

}

}

转载请注明出处51数据库 » java中导出word表格数据

软件
前端设计
程序设计
Java相关