java如何将数据导入Excel模板
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java如何将数据导入Excel模板相关的知识,希望对你有一定的参考价值。
参考技术A import java.io.BufferedReader;import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public static ArrayList<String> readTelExcel(String filepath)
try
//查询文件是否存在
File file = new File(filepath);
if (file.exists())
ArrayList<String> result = new ArrayList<String>();
//声明一个excel文件对象
Workbook wb = Workbook.getWorkbook(file);
//读取每一个工作薄,你也可能用wb.getSheets()得到全部工作薄
Sheet ws = wb.getSheet(0);
if (null != ws)
//取出所有行
int rows = ws.getRows();
for (int i = 0; i < rows; i++)
//读取第一列中的内容
String cell = ws.getCell(0, i).getContents();
if (null != cell)
result.add(cell.trim());
return result;
return null;
catch (BiffException e)
e.printStackTrace();
return null;
catch (IndexOutOfBoundsException e)
e.printStackTrace();
return null;
catch (IOException e)
e.printStackTrace();
return null;
参考技术B 引用poi jar包就行了 参考技术C 用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()
/**
* 创建新的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;
本回答被提问者采纳 参考技术D jxl或poi,百度搜一下怎么用吧
以上是关于java如何将数据导入Excel模板的主要内容,如果未能解决你的问题,请参考以下文章
EXCEL表格中如何将最新录入的一行数据导入另一个模板EXCEL中
如何在EXCEL的数据中导入到我自己设定的EXCEL模板中!?