乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > -java excel 导入的模板,java使用模板导出excel

-java excel 导入的模板,java使用模板导出excel

作者:乔山办公网日期:

返回目录:excel表格制作







用jxl吧··
lib包留个邮箱发给你,或者去搜索一下·很多下载
随便从个项目里给你找了个Excel操作的封装类

package hr.report;

import java.io.*;
import java.util.List;

import jxl.*;
import jxl.write.*;
import jxl.format.UnderlineStyle;

/**
* <p>
* Title:
* </p>
* <p>
* Description:
* </p>
* <p>
* Copyright: Copyright (c) 2008
* </p>
* <p>
* Company:
* </p>
*
* @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() {
}

/**
* 创建新的e69da5e6ba90e799bee5baa6335EXCEL 返回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;
}
}

相关阅读

  • <em>JAVA</em> POI 导出 EXCEL<em>模板</em>

  • 乔山办公网excel表格制作
  • poi或jxl都可以生成excel,给你说下jxl怎么生成的百吧,详细的api你可以从网上下载。//添加带有formatting的Number对象 jxl.write.NumberFormat nf = new jxl.write.NumberFormat("#.##"); //设置数字格式j
  • <em>java</em> <em>导出excel</em> 时报文件格

  • 乔山办公网excel表格制作
  • 看看后缀名是不是.XLS的如果不是要百另存为.XLS再试试打开excel,然后点击打开,度选择文件后,在选择打开右下角的下箭头"修复知打开" 如果还是不行的话,就是文件损坏了。建议用道
关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网