java导入excel模板_java如何将数据导入Excel模板

展开全部

用jxl吧··

lib包留个邮箱发给你,或者去搜索一下·很多下载32313133353236313431303231363533e4b893e5b19e31333262363635

随便从个项目里给你找了个Excel操作的封装类

package hr.report;

import java.io.*;

import java.util.List;

import jxl.*;

import jxl.write.*;

import jxl.format.UnderlineStyle;

/**

*

* Title:

*

*

* Description:

*

*

* Copyright: Copyright (c) 2008

*

*

* Company:

*

*

* @author not attributable

* @version 1.0

*/

public class ExcelTool {

private int col;

private int row;

private double numvalue;

private String stringvalue;

private java.util.Date datevalue;

private boolean boolvalue;

private String valuetype;

public ExcelTool() {

}

/**

* 创建新的EXCEL 返回WritableWorkbook对象

*

* @param excelFileName

* @return

* @throws java.lang.Exception

*/

public static jxl.write.WritableWorkbook NewExcel(String excelFilePath)

throws Exception {

OutputStream os = new FileOutputStream(excelFilePath);

jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);

return wwb;

}

/**

*

* @param excelFilePath

* @param sheetname

* @param sheetindex

* @param info

* 参数info

* @throws java.lang.Exception

*/

public static void NewExcel(String excelFilePath, String sheetname,

int sheetindex, ExcelTool info[]) throws Exception {

OutputStream os = new FileOutputStream(excelFilePath);

jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(os);

jxl.write.WritableSheet ws = ExcelTool.NewSheet(wwb, sheetname,

sheetindex);

if (info.length > 0) {

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

if (info[i].getValuetype().equals("number")) {

ExcelTool.EditExcel(ws, info[i].getCol(), info[i].getRow(),

info[i].getNumvalue());

} else if (info[i].getValuetype().equals("string")) {

ExcelTool.EditExcel(ws, info[i].getCol(), info[i].getRow(),

info[i].getStringvalue());

} else if (info[i].getValuetype().equals("boolean")) {

ExcelTool.EditExcel(ws, info[i].getCol(), info[i].getRow(),

info[i].isBoolvalue());

} else if (info[i].getValuetype().equals("date")) {

ExcelTool.EditExcel(ws, info[i].getCol(), info[i].getRow(),

info[i].getDatevalue());

}

}

}

wwb.write();

wwb.close();

}

/**

* 新建一个Sheet并返回该对象

*

* @param wwb

* @param sheetname

* @param index

* @return

* @throws java.lang.Exception

*/

public static jxl.write.WritableSheet NewSheet(

jxl.write.WritableWorkbook wwb, String sheetname, int index)

throws Exception {

jxl.write.WritableSheet ws = wwb.createSheet(sheetname, index);

return ws;

}

/**

* 新EXCEL的写入

*

* @param excelFilePath

* @return

* @throws java.lang.Exception

*/

public static void EditExcel(jxl.write.WritableSheet ws, int col, int row,

String value) throws Exception { // 字符串类型

jxl.write.Label labelC = new jxl.write.Label(col, row, value);

ws.addCell(labelC);

}

public static void EditExcel(jxl.write.WritableSheet ws, int col, int row,

double value) throws Exception { // 数字类型

jxl.write.Number labelN = new jxl.write.Number(col, row, value);

ws.addCell(labelN);

}

public static void EditExcel(jxl.write.WritableSheet ws, int col, int row,

boolean value) throws Exception { // bool型

jxl.write.Boolean labelB = new jxl.write.Boolean(col, row, false);

ws.addCell(labelB);

}

public static void EditExcel(jxl.write.WritableSheet ws, int col, int row,

java.util.Date value) throws Exception { // Date类型

jxl.write.DateTime labelDT = new jxl.write.DateTime(col, row, value);

ws.addCell(labelDT);

}

/**

* 操作EXCEL方法

*

* @param file

* 操作文件

* @param Sheet

* @param col

* 列

* @param row

* 行

* @param value

* 值

*/

public static void WriteExcel(File file, int Sheet, int col, int row,

String value) {

try {

// Method 1:创建可写入的Excel工作薄

jxl.Workbook rw = jxl.Workbook.getWorkbook(file);

jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(file, rw);

jxl.write.WritableSheet ws = wwb.getSheet(Sheet);

jxl.write.WritableCell wc = ws.getWritableCell(col, row);

// 判断单元格的类型, 做出相应的转化

if (wc.getType() == CellType.LABEL) {

// System.out.println("字符串类型");

Label l = (Label) wc;

l.setString(value);

} else if (wc.getType() == CellType.NUMBER) {

// System.out.println("数值类型");

jxl.write.Number a = (jxl.write.Number) wc;

a.setValue(Integer.parseInt(value));

}

// 写入Excel对象

wwb.write();

// 关闭可写入的Excel对象

wwb.close();

// 关闭只读的Excel对象

rw.close();

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 批量操作EXCEL方法

* @param file

* @param Sheet

* @param list for ExcelInfo

*/

public static void WriteExcels(File file, int Sheet, List list) {

try {

if (list != null && list.size() > 0) {

// Method 1:创建可写入的Excel工作薄

jxl.Workbook rw = jxl.Workbook.getWorkbook(file);

jxl.write.WritableWorkbook wwb = Workbook.createWorkbook(file,

rw);

jxl.write.WritableSheet ws = wwb.getSheet(0);

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

ExcelInfo einfo = new ExcelInfo();

einfo = (ExcelInfo) list.get(i);

jxl.write.WritableCell wc = ws.getWritableCell(

einfo.getX(), einfo.getY());

// 判断单元格的类型, 做出相应的转化

if (wc.getType() == CellType.LABEL) {

// System.out.println("字符串类型");

Label l = (Label) wc;

l.setString(einfo.getValue());

} else if (wc.getType() == CellType.NUMBER) {

// System.out.println("数值类型");

jxl.write.Number a = (jxl.write.Number) wc;

a.setValue(Integer.parseInt(einfo.getValue()));

}

}

// 写入Excel对象

wwb.write();

// 关闭可写入的Excel对象

wwb.close();

// 关闭只读的Excel对象

rw.close();

}

} catch (Exception e) {

e.printStackTrace();

}

}

public int getCol() {

return col;

}

public void setCol(int col) {

this.col = col;

}

public int getRow() {

return row;

}

public void setRow(int row) {

this.row = row;

}

public String getStringvalue() {

return stringvalue;

}

public void setStringvalue(String stringvalue) {

this.stringvalue = stringvalue;

}

public double getNumvalue() {

return numvalue;

}

public void setNumvalue(double numvalue) {

this.numvalue = numvalue;

}

public boolean getBoolvalue() {

return boolvalue;

}

public boolean isBoolvalue() {

return boolvalue;

}

public void setBoolvalue(boolean boolvalue) {

this.boolvalue = boolvalue;

}

public java.util.Date getDatevalue() {

return datevalue;

}

public void setDatevalue(java.util.Date datevalue) {

this.datevalue = datevalue;

}

public String getValuetype() {

return valuetype;

}

public void setValuetype(String valuetype) {

this.valuetype = valuetype;

}

// 必须要做的//

// 写入Exel工作表

// wwb.write();

// 关闭Excel工作薄对象

// wwb.close();

//

/**

* 将来用到的可扩展的封装方法例子

*

*

* //添加带有字型Formatting的对象 jxl.write.WritableFont wf = new

* jxl.write.WritableFont(WritableFont.TIMES, 18, WritableFont.BOLD, true);

* jxl.write.WritableCellFormat wcfF = new jxl.write.WritableCellFormat(wf);

* jxl.write.Label labelCF = new jxl.write.Label(10, 1, "This is a Label

* Cell", wcfF); ws.addCell(labelCF); //添加带有字体颜色Formatting的对象

* jxl.write.WritableFont wfc = new

* jxl.write.WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD,

* false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);

* jxl.write.WritableCellFormat wcfFC = new

* jxl.write.WritableCellFormat(wfc); jxl.write.Label labelCFC = new

* jxl.write.Label(10, 2, "This is a Label Cell", wcfFC);

* ws.addCell(labelCFC);

*

* //添加带有formatting的Number对象 jxl.write.NumberFormat nf = new

* jxl.write.NumberFormat("#.##"); jxl.write.WritableCellFormat wcfN = new

* jxl.write.WritableCellFormat(nf); jxl.write.Number labelNF = new

* jxl.write.Number(1, 1, 3.1415926, wcfN); ws.addCell(labelNF);

*

* //添加带有formatting的DateFormat对象 jxl.write.DateFormat df = new

* jxl.write.DateFormat("dd MM yyyy hh:mm:ss"); jxl.write.WritableCellFormat

* wcfDF = new jxl.write.WritableCellFormat(df); jxl.write.DateTime labelDTF =

* new jxl.write.DateTime(1, 3, new java.util.Date(), wcfDF);

* ws.addCell(labelDTF);

*/

}

class ExcelInfo {

private int x = 0; //列

private int y = 0; //行

private String value = "";

public String getValue() {

return value;

}

public void setValue(String value) {

this.value = value;

}

public int getX() {

return x;

}

public void setX(int x) {

this.x = x;

}

public int getY() {

return y;

}

public void setY(int y) {

this.y = y;

}

}

本回答由提问者推荐

2Q==

已赞过

已踩过<

你对这个回答的评价是?

评论

收起

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值