我參考的博客http://blog.csdn.net/wj123446/article/details/71110185java
主方法:apache
package util;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;數組
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;ide
public class Main extends JFrame implements ActionListener{
public void init(){
//建立文本框
JPanel pan = new JPanel();
this.add(pan);
this.placeComponents(pan);
this.setSize(350, 200);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
}
public static void main(String[] args) throws IOException {
Main main = new Main();
main.init();
}
JTextField ReadPath;
JTextField WritePath ;
private void placeComponents(JPanel panel) {佈局
/* 佈局部分咱們這邊很少作介紹
* 這邊設置佈局爲 null
*/
panel.setLayout(null);
/* 這個方法定義了組件的位置。
* setBounds(x, y, width, height)
* x 和 y 指定左上角的新位置,由 width 和 height 指定新的大小。
*/
// 建立 JLabel
JLabel InName = new JLabel("商戶地址");
InName.setBounds(10,20,80,25);
panel.add(InName);this
/*
* 建立文本域用於用戶輸入商戶信息地址
*/
ReadPath = new JTextField(20);
ReadPath.setBounds(100,20,165,25);
panel.add(ReadPath);
JLabel OutName = new JLabel("模版地址");
OutName.setBounds(10,50,80,25);
panel.add(OutName);
/*
* 建立文本域用於用戶輸入模版地址
*/
WritePath = new JTextField(20);
WritePath.setBounds(100,50,165,25);
panel.add(WritePath);
// 建立點擊按鈕
JButton downButton = new JButton("start");
downButton.setBounds(10, 80, 80, 25);
downButton.addActionListener(this);
panel.add(downButton);
}.net
private void start(String readPath,String writePath) throws IOException{
ReadExcel re = new ReadExcel();
File file = new File(readPath);
WriterExcel we = new WriterExcel(writePath);
List<String[][]> result = new ArrayList<String[][]>();
//忽略第一行 列名
result = re.getData(file, 0);
//有多少行
int row = result.get(0).length;
//寫入 傳入參數row 不傳column column是不肯定的
we.writeEx(row,result.get(0));
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String readPath = ReadPath.getText();
String writePath = WritePath.getText();
try {
this.start(readPath,writePath);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}
excel
讀取Excel文件方法:orm
package util;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
* @author shirenchuang
*
*/
public class ReadExcel {
/*private String fileUrl;
public ReadExecl(String fileUrl) {
// TODO Auto-generated constructor stub
this.fileUrl = fileUrl;
}*/
// File file = new File(fileUrl);
/**
* 讀取Excel的內容,第一維數組存儲的是一行中格列的值,二維數組存儲的是多少個行
* @param file 讀取數據的源Excel
* @param ignoreRows 讀取數據忽略的行數,比喻行頭不須要讀入 忽略的行數爲1
* @return 讀出的Excel中數據的內容
* @throws FileNotFoundException
* @throws IOException
*/
@SuppressWarnings("deprecation")
public static List<String[][]> getData(File file,int ignoreRows) throws IOException{
//返回全部工做表的數據
List<String[][]> result = new ArrayList<String[][]>();
//獲得文件的輸入流
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
POIFSFileSystem fs = new POIFSFileSystem(in);
HSSFWorkbook wb = new HSSFWorkbook(fs); //建立輸入流的excel文件
HSSFCell cell = null;
System.out.println("NumOfSheets: "+wb.getNumberOfSheets());
//多個工做表
for(int i=0;i<wb.getNumberOfSheets();i++){
//獲得工做表
HSSFSheet hf = wb.getSheetAt(i);
//一個工做表的數據 記得加上1 好比excel加上第一行的列名 總共66 可是getLastRowNum()是65 把列名也算上
String[][] rowResult = new String[hf.getLastRowNum()+1][7];
System.out.println("LastRowNum: "+hf.getLastRowNum());
//每一個工做表的多行
for(int rownumber = ignoreRows; rownumber<=hf.getLastRowNum();rownumber++){
//獲得的row的行數不肯定的 若是那一行後面有空格 有可能會忽略空格列
HSSFRow row = hf.getRow(rownumber);
if (row == null) {
continue;
}
//某一行的數據
String[] colResult = new String[row.getLastCellNum()];
//獲得一行的多個列
/**
* 這裏有個問題 就是row.getLastCellNum()有個狀況獲得的不許確
* 例子:EXECL總共7列數據 可是實際上最後幾列有的爲空,它會默認把空的列不計入列值;
* 致使的錯誤就是你調用寫入方法的時候會有用的列值 會出錯;
*
*/
for(short colnumber = 0;colnumber<row.getLastCellNum();colnumber++){
String value="";
cell=row.getCell(colnumber);
//將cell裝換類型
if(cell!=null){
//cell.setEncoding(HSSFCell.ENCODING_UTF_16);
switch(cell.getCellType()){
case HSSFCell.CELL_TYPE_STRING:
value = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
if (HSSFDateUtil.isCellDateFormatted(cell)) {
Date date = cell.getDateCellValue();
if (date != null) {
value = new SimpleDateFormat("yyyy-MM-dd")
.format(date);
} else {
value = "";
}
} else {
value = new DecimalFormat("0").format(cell
.getNumericCellValue());
}
break;
case HSSFCell.CELL_TYPE_FORMULA:
// 導入時若是爲公式生成的數據則無值
if (!cell.getStringCellValue().equals("")) {
value = cell.getStringCellValue();
} else {
value = cell.getNumericCellValue() + "";
}
break;
case HSSFCell.CELL_TYPE_BLANK:
break;
case HSSFCell.CELL_TYPE_ERROR:
value = "";
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
value = (cell.getBooleanCellValue() == true ? "Y"
: "N");
break;
default:
value = "";
}//switch
}//if
if (colnumber == 0 && value.trim().equals("")) {
// break;
}
colResult[colnumber]=rightTrim(value);
}//for()列
rowResult[rownumber]=colResult;
}//for() 行
if(rowResult!=null)
result.add(rowResult);
}//for工做表
in.close();
return result;
}
/**
* 去掉字符串右邊的空格
* @param str 要處理的字符串
* @return 處理後的字符串
*/
public static String rightTrim(String str) {
if (str == null) {
return "";
}
int length = str.length();
for (int i = length - 1; i >= 0; i--) {
if (str.charAt(i) != 0x20) {
break;
}
length--;
}
return str.substring(0, length);
}
} blog
批量寫Excel方法後並另存到E:\test中。
package util;
import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.CellType;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
/**
* @author
*
*/
public class WriterExcel {
private static String writeUrl ="";
private static int NUM = 0;
public String getWriteUrl() {
return writeUrl;
}
public void setWriteUrl(String writeUrl) {
this.writeUrl = writeUrl;
}
public WriterExcel(String writeUrl ) {
// TODO Auto-generated constructor stub
this.writeUrl= writeUrl;
}
public static void writeCell(int sheetNum,int col,int row,String[][] data) throws BiffException, IOException, WriteException{
String file =writeUrl;
WritableWorkbook wwb = null;
Label label = null;
// Excel得到文件
Workbook wb = Workbook.getWorkbook(new File(file));
// 打開一個文件的副本,而且指定數據寫回到原文件
wwb = Workbook.createWorkbook(new File(file),wb);
WritableSheet ws = wwb.getSheet(sheetNum);
WritableCell wc = ws.getWritableCell(col,row);
if (ws != null) {
//判斷單元格的類型,作出相應的轉換;找到相應的工做表填寫單元格
if(wc.getType() == CellType.LABEL && sheetNum == 0)
{
label = (Label)wc;
label.setString(data[NUM][0]);
System.out.println(data[NUM][0]);
}else if(wc.getType() == CellType.LABEL && sheetNum == 1){
label = (Label)wc;
label.setString(data[NUM][1]);
System.out.println(data[NUM][1]);
}else if(wc.getType() == CellType.LABEL && sheetNum == 2){
label = (Label)wc;
label.setString(data[NUM][2]);
System.out.println(data[NUM][2]);
}
}
// 從內存中寫入到文件
wwb.write();
wwb.close();
wb.close();
System.out.println("路徑爲:" + file + "的工做簿寫入數據成功!");
}
/**
*
* 另存填寫的excel表格,命名規則按,1.xsl ...
* **/
private static void copy(String inname,String outname) throws BiffException, IOException, WriteException {
// Excel得到文件
Workbook workbook = Workbook.getWorkbook(new File(inname));
WritableWorkbook outExcel =Workbook.createWorkbook(new File(outname),workbook);
outExcel.write();
outExcel.close();
}
/**
*
* 這是單純的寫EXCEL表格
* **/
public static void writeEx(int row,String[][] data){
while(NUM<=row){
NUM++;
try {
writeCell(0,1,3,data);
writeCell(1,1,2,data);
writeCell(2,1,3,data);
copy(writeUrl,"E:/test/"+NUM+".xls");
} catch (Exception e) {
System.out.println(e.getMessage());
} finally {}
}
}
}