「java输出execl」java输出数字

博主:adminadmin 2023-01-24 18:09:10 455

今天给各位分享java输出execl的知识,其中也会对java输出数字进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!

本文目录一览:

java怎么导出excel表格

可以使用POI开源的api:

1.首先下载poi-3.6-20091214.jar,下载地址如下:

2.Student.java

import java.util.Date;

public class Student

{

private int id;

private String name;

private int age;

private Date birth;

public Student()

{

}

public Student(int id, String name, int age, Date birth)

{

this.id = id;

this.name = name;

this.age = age;

this.birth = birth;

}

public int getId()

{

return id;

}

public void setId(int id)

{

this.id = id;

}

public String getName()

{

return name;

}

public void setName(String name)

{

this.name = name;

}

public int getAge()

{

return age;

}

public void setAge(int age)

{

this.age = age;

}

public Date getBirth()

{

return birth;

}

public void setBirth(Date birth)

{

this.birth = birth;

}

}

3.CreateSimpleExcelToDisk.java

import java.io.FileOutputStream;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.List;

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;

public class CreateSimpleExcelToDisk

{

/**

* @功能:手工构建一个简单格式的Excel

*/

private static ListStudent getStudent() throws Exception

{

List list = new ArrayList();

SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");

Student user1 = new Student(1, "张三", 16, df.parse("1997-03-12"));

Student user2 = new Student(2, "李四", 17, df.parse("1996-08-12"));

Student user3 = new Student(3, "王五", 26, df.parse("1985-11-12"));

list.add(user1);

list.add(user2);

list.add(user3);

return list;

}

public static void main(String[] args) throws Exception

{

// 第一步,创建一个webbook,对应一个Excel文件

HSSFWorkbook wb = new HSSFWorkbook();

// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet

HSSFSheet sheet = wb.createSheet("学生表一");

// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short

HSSFRow row = sheet.createRow((int) 0);

// 第四步,创建单元格,并设置值表头 设置表头居中

HSSFCellStyle style = wb.createCellStyle();

style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式

HSSFCell cell = row.createCell((short) 0);

cell.setCellValue("学号");

cell.setCellStyle(style);

cell = row.createCell((short) 1);

cell.setCellValue("姓名");

cell.setCellStyle(style);

cell = row.createCell((short) 2);

cell.setCellValue("年龄");

cell.setCellStyle(style);

cell = row.createCell((short) 3);

cell.setCellValue("生日");

cell.setCellStyle(style);

// 第五步,写入实体数据 实际应用中这些数据从数据库得到,

List list = CreateSimpleExcelToDisk.getStudent();

for (int i = 0; i list.size(); i++)

{

row = sheet.createRow((int) i + 1);

Student stu = (Student) list.get(i);

// 第四步,创建单元格,并设置值

row.createCell((short) 0).setCellValue((double) stu.getId());

row.createCell((short) 1).setCellValue(stu.getName());

row.createCell((short) 2).setCellValue((double) stu.getAge());

cell = row.createCell((short) 3);

cell.setCellValue(new SimpleDateFormat("yyyy-mm-dd").format(stu

.getBirth()));

}

// 第六步,将文件存到指定位置

try

{

FileOutputStream fout = new FileOutputStream("E:/students.xls");

wb.write(fout);

fout.close();

}

catch (Exception e)

{

e.printStackTrace();

}

}

}

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

    }

    

}

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

}

}

Java是由Sun Microsystems公司推出的Java面向对象程序设计语言(以下简称Java语言)和Java平台的总称。由James Gosling和同事们共同研发,并在1995年正式推出。Java最初被称为Oak,是1991年为消费类电子产品的嵌入式芯片而设计的。1995年更名为Java,并重新设计用于开发Internet应用程序。

用Java实现的HotJava浏览器(支持Java applet)显示了Java的魅力:跨平台、动态Web、Internet计算。从此,Java被广泛接受并推动了Web的迅速发展,常用的浏览器均支持Javaapplet。另一方面,Java技术也不断更新。Java自面世后就非常流行,发展迅速,对C++语言形成有力冲击。在全球云计算和移动互联网的产业环境下,Java更具备了显著优势和广阔前景。2010年Oracle公司收购Sun Microsystems。

怎么用java实现导出excel

/**

 * @author liuwu

 * Excel的导入与导出

 */

@SuppressWarnings({ "unchecked" })

public class ExcelOperate {

/**

 * @author liuwu

 * 这是一个通用的方法,利用了JAVA的反射机制,

 * 可以将放置在JAVA集合中并且符合一定条件的数据以EXCEL的形式输出到指定IO设备上

 * @param title 表格标题名

 * @param headers 表格属性列名数组

 * @param dataset 需要显示的数据集合,集合中一定要放置符合javabean风格的类的对象。

 *  此方法支持的 javabean属性【数据类型有java基本数据类型及String,Date,byte[](图片转成字节码)】

 * @param out 与输出设备关联的流对象,可以将EXCEL文档导出到本地文件或者网络中

 * @param pattern  如果有时间数据,设定输出格式。默认为"yyy-MM-dd"

 * @throws IOException 

 */

public static void exportExcel(String title, String[] headers,Collection? dataset, OutputStream out, String pattern) throws IOException {

// 声明一个工作薄

HSSFWorkbook workbook = new HSSFWorkbook();

// 生成一个表格

HSSFSheet sheet = workbook.createSheet(title);

// 设置表格默认列宽度为15个字节

sheet.setDefaultColumnWidth((short) 20);

// 生成一个样式

HSSFCellStyle style = workbook.createCellStyle();

// 设置这些样式

style.setFillForegroundColor(HSSFColor.SKY_BLUE.index);

style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

style.setBorderBottom(HSSFCellStyle.BORDER_THIN);

style.setBorderLeft(HSSFCellStyle.BORDER_THIN);

style.setBorderRight(HSSFCellStyle.BORDER_THIN);

style.setBorderTop(HSSFCellStyle.BORDER_THIN);

style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

// 生成一个字体

HSSFFont font = workbook.createFont();

font.setColor(HSSFColor.VIOLET.index);

font.setFontHeightInPoints((short) 12);

font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);

// 把字体应用到当前的样式

style.setFont(font);

// 生成并设置另一个样式

HSSFCellStyle style2 = workbook.createCellStyle();

style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index);

style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);

style2.setBorderBottom(HSSFCellStyle.BORDER_THIN);

style2.setBorderLeft(HSSFCellStyle.BORDER_THIN);

style2.setBorderRight(HSSFCellStyle.BORDER_THIN);

style2.setBorderTop(HSSFCellStyle.BORDER_THIN);

style2.setAlignment(HSSFCellStyle.ALIGN_CENTER);

style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);

// 生成另一个字体

HSSFFont font2 = workbook.createFont();

font2.setBoldweight(HSSFFont.BOLDWEIGHT_NORMAL);

// 把字体应用到当前的样式

style2.setFont(font2);

// 产生表格标题行

HSSFRow row = sheet.createRow(0);

for (short i = 0; i  headers.length; i++) {

HSSFCell cell = row.createCell(i);

cell.setCellStyle(style);

HSSFRichTextString text = new HSSFRichTextString(headers[i]);

cell.setCellValue(text);

}

// 遍历集合数据,产生数据行

Iterator? it = dataset.iterator();

int index = 0;

while (it.hasNext()) {

index++;

row = sheet.createRow(index);

Object t =  it.next();

// 利用反射,根据javabean属性的先后顺序,动态调用getXxx()方法得到属性值

Field[] fields = t.getClass().getDeclaredFields();

for (short i = 0; i  fields.length; i++) {

HSSFCell cell = row.createCell(i);

cell.setCellStyle(style2);

Field field = fields[i];

String fieldName = field.getName();

String getMethodName = "get"

+ fieldName.substring(0, 1).toUpperCase()

+ fieldName.substring(1);//注意 实体get Set不要自己改名字不然反射会有问题

try {

Class tCls = t.getClass();

Method getMethod = tCls.getMethod(getMethodName,new Class[] {});

Object value = getMethod.invoke(t, new Object[] {});

HSSFRichTextString richString = new HSSFRichTextString(value.toString());

HSSFFont font3 = workbook.createFont();

font3.setColor(HSSFColor.BLUE.index);

richString.applyFont(font3);

cell.setCellValue(richString);

} catch (SecurityException e) {

e.printStackTrace();

e=null;

} catch (NoSuchMethodException e) {

e.printStackTrace();

e=null;

} catch (IllegalArgumentException e) {

e.printStackTrace();

e=null;

} catch (IllegalAccessException e) {

e.printStackTrace();

e=null;

} catch (InvocationTargetException e) {

e.printStackTrace();

e=null;

} finally {

// 清理资源

}

}

}

try {

workbook.write(out);

} catch (IOException e) {

e.printStackTrace();

e=null;

}

}

}

java 通用导出一个excel

java导出Excel通用方法,只需要一个list 集合。

package oa.common.utils;

import java.io.OutputStream;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import java.lang.reflect.Field;

import jxl.Workbook;

import jxl.format.Alignment;

import jxl.format.Border;

import jxl.format.BorderLineStyle;

import jxl.format.VerticalAlignment;

import jxl.write.Label;

import jxl.write.WritableCellFormat;

import jxl.write.WritableFont;

import jxl.write.WritableSheet;

import jxl.write.WritableWorkbook;

/***

* @author lsf

*/

public class ExportExcel {

/***************************************************************************

* @param fileName EXCEL文件名称

* @param listTitle EXCEL文件第一行列标题集合

* @param listContent EXCEL文件正文数据集合

* @return

*/

public final static String exportExcel(String fileName,String[] Title, ListObject listContent) {

String result="系统提示:Excel文件导出成功!";

// 以下开始输出到EXCEL

try {

//定义输出流,以便打开保存对话框______________________begin

HttpServletResponse response=ServletActionContext.getResponse();

OutputStream os = response.getOutputStream();// 取得输出流

response.reset();// 清空输出流

response.setHeader("Content-disposition", "attachment; filename="+ new String(fileName.getBytes("GB2312"),"ISO8859-1"));

// 设定输出文件头

response.setContentType("application/msexcel");// 定义输出类型

//定义输出流,以便打开保存对话框_______________________end

/** **********创建工作簿************ */

WritableWorkbook workbook = Workbook.createWorkbook(os);

/** **********创建工作表************ */

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

/** **********设置纵横打印(默认为纵打)、打印纸***************** */

jxl.SheetSettings sheetset = sheet.getSettings();

sheetset.setProtected(false);

/** ************设置单元格字体************** */

WritableFont NormalFont = new WritableFont(WritableFont.ARIAL, 10);

WritableFont BoldFont = new WritableFont(WritableFont.ARIAL, 10,WritableFont.BOLD);

/** ************以下设置三种单元格样式,灵活备用************ */

// 用于标题居中

WritableCellFormat wcf_center = new WritableCellFormat(BoldFont);

wcf_center.setBorder(Border.ALL, BorderLineStyle.THIN); // 线条

wcf_center.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直对齐

wcf_center.setAlignment(Alignment.CENTRE); // 文字水平对齐

wcf_center.setWrap(false); // 文字是否换行

// 用于正文居左

WritableCellFormat wcf_left = new WritableCellFormat(NormalFont);

wcf_left.setBorder(Border.NONE, BorderLineStyle.THIN); // 线条

wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE); // 文字垂直对齐

wcf_left.setAlignment(Alignment.LEFT); // 文字水平对齐

wcf_left.setWrap(false); // 文字是否换行

/** ***************以下是EXCEL开头大标题,暂时省略********************* */

//sheet.mergeCells(0, 0, colWidth, 0);

//sheet.addCell(new Label(0, 0, "XX报表", wcf_center));

/** ***************以下是EXCEL第一行列标题********************* */

for (int i = 0; i Title.length; i++) {

sheet.addCell(new Label(i, 0,Title[i],wcf_center));

}

/** ***************以下是EXCEL正文数据********************* */

Field[] fields=null;

int i=1;

for(Object obj:listContent){

fields=obj.getClass().getDeclaredFields();

int j=0;

for(Field v:fields){

v.setAccessible(true);

Object va=v.get(obj);

if(va==null){

va="";

}

sheet.addCell(new Label(j, i,va.toString(),wcf_left));

j++;

}

i++;

}

/** **********将以上缓存中的内容写到EXCEL文件中******** */

workbook.write();

/** *********关闭文件************* */

workbook.close();

} catch (Exception e) {

result="系统提示:Excel文件导出失败,原因:"+ e.toString();

System.out.println(result);

e.printStackTrace();

}

return result;

}

}

测试:

/**

* 导出excel

* @return

*/

public String excelPage(){

ExportExcel excel=new ExportExcel();

String str="";

try {

str = new String(getHTTP.getRequest().getParameter("wineOrg.orgName").getBytes("iso8859-1"),"UTF-8");

} catch (UnsupportedEncodingException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

wineOrg.setOrgName(str);

ListObject li=service.exportExcel(wineOrg);

String[] Title={"机构ID","会员编号","类别","名称","省ID","省名称","城市ID","城市名称","详细地址","联系人","性别","联系手机","联系电话","传真","邮箱","QQ","生日","积分","客户等级","现金账户余额","结算方式","客户类型","购买次数","购买支数","创建人ID","创建人姓名","create_time","del","STS","备注","负责人ID","负责人姓名","审核标识","审核人ID ","审核人姓名","审核日期","分配人ID","分配人姓名","分配日期","修改人ID","修改人姓名 ","修改时间"};

excel.exportExcel("客户资料信息.xls",Title, li);

return SUCCESS;

}

java输出execl的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java输出数字、java输出execl的信息别忘了在本站进行查找喔。