1. 程式人生 > >java導入excle表格,並且對表格進行相應的修改,並對表格數據進行整理,最後導出本地表格等一系列操作

java導入excle表格,並且對表格進行相應的修改,並對表格數據進行整理,最後導出本地表格等一系列操作

對象 tabs stream span 指數 ring vat 14. int

1.首先創建一個java項目

2.導入以下jar包

技術分享圖片

3.代碼如下

public class auto_date {
private static List<List<String>> readExcel(File file) throws Exception {
// 創建輸入流,讀取Excel
InputStream is = new FileInputStream(file.getAbsolutePath());
// jxl提供的Workbook類
Workbook wb = Workbook.getWorkbook(is);
// 只有一個sheet,直接處理
//創建一個Sheet對象
Sheet sheet = wb.getSheet(0);
// 得到所有的行數
int rows = sheet.getRows();
// 所有的數據
List<List<String>> allData = new ArrayList<List<String>>();
// 越過第一行 它是列名稱
for (int j = 1; j < rows; j++) {
List<String> oneData = new ArrayList<String>();
// 得到每一行的單元格的數據
Cell[] cells = sheet.getRow(j);
for (int k = 0; k < cells.length; k++) {
oneData.add(cells[k].getContents().trim());
}
// 存儲每一條數據
allData.add(oneData);
// 打印出每一條數據
//System.out.println(oneData);
}
return allData;
}
public static void main(String[] args) {
File file = new File("F://m//1.xls");
//42列
//3337行
try {
List<List<String>> allData=readExcel(file);
//System.out.println("總數:"+allData.size());//總行數
/**
* 創建excle表格
*/
// 第一步,創建一個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); // 創建一個居中格式
// HSSFRow row1 = sheet.createRow(0);
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("所有小麥特征表");
sheet.addMergedRegion(new CellRangeAddress(0,0,0,20));
HSSFRow row2 = sheet.createRow(1);
row2.createCell(0).setCellValue("品種名稱");
row2.createCell(1).setCellValue("生態類型");
row2.createCell(2).setCellValue("生育期");
row2.createCell(3).setCellValue("苗性");
row2.createCell(4).setCellValue("葉色");
row2.createCell(5).setCellValue("分蘗力");
row2.createCell(6).setCellValue("株型");
row2.createCell(7).setCellValue("株高");
row2.createCell(8).setCellValue("株高");
row2.createCell(9).setCellValue("穗形");
row2.createCell(10).setCellValue("芒");
row2.createCell(11).setCellValue("殼色");
row2.createCell(12).setCellValue("粒色");
row2.createCell(13).setCellValue("硬度");
row2.createCell(14).setCellValue("籽粒飽滿度");
row2.createCell(15).setCellValue("畝穗數");
row2.createCell(16).setCellValue("穗粒數");
row2.createCell(17).setCellValue("千粒重");
row2.createCell(18).setCellValue("熟相");
row2.createCell(19).setCellValue("抗倒性");
row2.createCell(20).setCellValue("抗旱性");
row2.createCell(21).setCellValue("抗寒性1");
row2.createCell(22).setCellValue("抗寒性2");
row2.createCell(23).setCellValue("粗蛋白質");
row2.createCell(24).setCellValue("濕面筋");
row2.createCell(25).setCellValue("降落數值");
row2.createCell(26).setCellValue("沈澱指數");
row2.createCell(27).setCellValue("吸水量");
row2.createCell(28).setCellValue("形成時間");
row2.createCell(29).setCellValue("穩定時間");
row2.createCell(30).setCellValue("弱化度");
row2.createCell(31).setCellValue("出粉率");
row2.createCell(32).setCellValue("延伸性");
row2.createCell(33).setCellValue("拉伸能量");
row2.createCell(34).setCellValue("拉伸面積");
row2.createCell(35).setCellValue("最大拉伸阻力");
row2.createCell(36).setCellValue("容重");
row2.createCell(37).setCellValue("節水性指數1");
row2.createCell(38).setCellValue("節水性指數2");
row2.createCell(39).setCellValue("節水性1");
row2.createCell(40).setCellValue("節水性2");
row2.createCell(41).setCellValue("抗病性1");
row2.createCell(42).setCellValue("抗病性2");

/**
* 導出txt文件
*/
// File writename = new File("G://2.txt"); // 相對路徑,如果沒有則要建立一個新的output。txt文件
// writename.createNewFile(); // 創建新文件
// BufferedWriter out = new BufferedWriter(new FileWriter(writename));
// out.write("寫入文件\r\n"); // \r\n即為換行

//System.out.println(allData);//輸出全部
for (int i = 0; i < allData.size(); i++) {
List<String> list=allData.get(i);
HSSFRow row3 = sheet.createRow(i+2);
HSSFCell col0 = row3.createCell(0);
col0.setCellValue(list.get(0));
/**
* 導出txt文件
*/
// out.write("**************************************"+"\r\n");
// out.write("第 "+i+" 個"+"品種名稱: "+list.get(0)+"\r\n");
//*************************
//System.out.println(allData.get(i));//逐條輸出
//System.out.println(list.get(0));
for (int j = 0; j <list.size(); j++) {
String str=list.get(j);

String newstr=str.replace(",", "。");
String newstr1=newstr.replace(";","。");
String newstr2=newstr1.replace("、","。");
String newstr3=newstr2.replace(";","。");
String newstr4=newstr3.replace(",","。");
//System.out.println(newstr1);
String[] temp;
String delimeter = "。"; // 指定分割字符
temp = newstr4.split(delimeter); // 分割字符串
// 普通 for 循環
for(int q =0; q < temp.length ; q++){
String disease="";
//生態類型
HSSFCell col11 = row3.createCell(1);
//生育周期
HSSFCell col12 = row3.createCell(2);
//苗性
HSSFCell col13 = row3.createCell(3);
//葉色
HSSFCell col14 = row3.createCell(4);
//分蘗力
HSSFCell col15 = row3.createCell(5);
//穗形
HSSFCell col16 = row3.createCell(6);
//芒
HSSFCell col17 = row3.createCell(7);
//殼色
HSSFCell col18 = row3.createCell(8);
//芒
HSSFCell col19 = row3.createCell(9);
//殼
HSSFCell col110 = row3.createCell(10);
//粒色
HSSFCell col111 = row3.createCell(11);
//硬度
HSSFCell col112 = row3.createCell(12);
//籽粒飽滿度
HSSFCell col113 = row3.createCell(13);
//畝穗數
HSSFCell col114 = row3.createCell(14);
//穗粒數
HSSFCell col115 = row3.createCell(15);
//千粒重
HSSFCell col116 = row3.createCell(16);
//熟相
HSSFCell col117 = row3.createCell(17);
//抗倒性
HSSFCell col118 = row3.createCell(18);
//抗旱性
HSSFCell col119 = row3.createCell(19);
//抗寒性
HSSFCell col120 = row3.createCell(20);
HSSFCell col121 = row3.createCell(21);
HSSFCell col122 = row3.createCell(22);
HSSFCell col123 = row3.createCell(23);
HSSFCell col124 = row3.createCell(24);
HSSFCell col125 = row3.createCell(25);
HSSFCell col126 = row3.createCell(26);
HSSFCell col127 = row3.createCell(27);
HSSFCell col128 = row3.createCell(28);
HSSFCell col129 = row3.createCell(29);
HSSFCell col130 = row3.createCell(30);
HSSFCell col131 = row3.createCell(31);
HSSFCell col132 = row3.createCell(32);
HSSFCell col133 = row3.createCell(33);
HSSFCell col134 = row3.createCell(34);
HSSFCell col135 = row3.createCell(35);
HSSFCell col136 = row3.createCell(36);
HSSFCell col137 = row3.createCell(37);
HSSFCell col138 = row3.createCell(38);
HSSFCell col139 = row3.createCell(39);
HSSFCell col140 = row3.createCell(40);
HSSFCell col141 = row3.createCell(41);
HSSFCell col142 = row3.createCell(42);
HSSFCell col143 = row3.createCell(43);
for(int r =0; r < temp.length ; r++){
if (temp[r].contains("春性")==true||temp[r].contains("冬性")==true) {

col11.setCellValue(temp[r]);
}
if (temp[r].contains("生育期")==true) {
col12.setCellValue(temp[r]);
}
if (temp[r].contains("幼苗")==true) {
col13.setCellValue(temp[r]);
}
if (temp[r].contains("葉色")==true) {
col14.setCellValue(temp[r]);
}
if (temp[r].contains("分蘗力")==true) {
col15.setCellValue(temp[r]);
}
if (temp[r].contains("株型")==true) {
col16.setCellValue(temp[r]);
}
if (temp[r].contains("株高")==true) {
col17.setCellValue(temp[r]);
}
if (temp[r].contains("穗紡錘")==true||temp[r].contains("穗長方")==true||temp[r].contains("穗棍棒")==true
||temp[r].contains("紡錘型")==true||temp[r].contains("棍棒形")==true
||temp[r].contains("穗型長方形")==true||temp[r].contains("長方穗型")==true) {
col19.setCellValue(temp[r]);
}
if (temp[r].contains("芒")==true) {
col110.setCellValue(temp[r]);
}
if (temp[r].contains("殼")==true) {
col111.setCellValue(temp[r]);
}
if (temp[r].contains("紅粒")==true||temp[r].contains("藍粒")==true||
temp[r].contains("白粒")==true||temp[r].contains("黑粒")==true||temp[r].contains("粒紅")==true
||temp[r].contains("粒黑")==true||temp[r].contains("粒白")==true) {
col112.setCellValue(temp[r]);
}
if (temp[r].contains("硬質")==true) {
col113.setCellValue(temp[r]);
}
if (temp[r].contains("飽滿")==true) {
col114.setCellValue(temp[r]);
}
if (temp[r].contains("畝穗數")==true) {
col115.setCellValue(temp[r]);
}
if (temp[r].contains("穗粒數")==true) {
col116.setCellValue(temp[r]);
}
if (temp[r].contains("千粒重")==true) {
col117.setCellValue(temp[r]);
}
if (temp[r].contains("熟相")==true) {
col118.setCellValue(temp[r]);
}
if (temp[r].contains("抗倒性")==true) {
col119.setCellValue(temp[r]);
}
if (temp[r].contains("抗旱性")==true) {
col120.setCellValue(temp[r]);
}
if (temp[r].contains("抗寒性")==true) {
col121.setCellValue(temp[r]);
}
// if (temp[r].contains("抗寒性")==true) {
// col122.setCellValue(temp[r]);
// }
if (temp[r].contains("粗蛋白質")==true) {
col123.setCellValue(temp[r]);
}
if (temp[r].contains("濕面筋")==true) {
col124.setCellValue(temp[r]);
}
if (temp[r].contains("降落數值")==true) {
col125.setCellValue(temp[r]);
}
if (temp[r].contains("沈澱指數")==true) {
col126.setCellValue(temp[r]);
}
if (temp[r].contains("吸水量")==true||temp[r].contains("吸水率")==true) {
col127.setCellValue(temp[r]);
}
if (temp[r].contains("形成時間")==true) {
col128.setCellValue(temp[r]);
}
if (temp[r].contains("穩定時間")==true) {
col129.setCellValue(temp[r]);
}
if (temp[r].contains("弱化度")==true) {
col130.setCellValue(temp[r]);
}
if (temp[r].contains("出粉率")==true) {
col131.setCellValue(temp[r]);
}
if (temp[r].contains("延伸性")==true) {
col132.setCellValue(temp[r]);
}
if (temp[r].contains("拉伸能量")==true) {
col133.setCellValue(temp[r]);
}
if (temp[r].contains("拉伸面積")==true) {
col134.setCellValue(temp[r]);
}
if (temp[r].contains("最大拉伸阻力")==true) {
col135.setCellValue(temp[r]);
}
if (temp[r].contains("容重")==true) {
col136.setCellValue(temp[r]);
}
if (temp[r].contains("節水指數")==true) {
col137.setCellValue(temp[r]);
}
// if (temp[r].contains("節水指數2")==true) {
// col138.setCellValue(temp[r]);
// }
if (temp[r].contains("節水性")==true) {
col139.setCellValue(temp[r]);
}
// if (temp[r].contains("節水性2")==true) {
// col140.setCellValue(temp[r]);
// }
/**
* 各種病癥
*/
if(temp[r].contains("抗病鑒定")==true){
disease+=temp[r]+":";
}
if(temp[r].contains("葉銹病")==true){
disease+=temp[r]+",";
}
if(temp[r].contains("條銹病")==true){
disease+=temp[r]+",";
}
if(temp[r].contains("白粉病")==true){
disease+=temp[r]+",";
}
if(temp[r].contains("赤黴病")==true){
disease+=temp[r]+",";
}
if(temp[r].contains("抗條銹病")==true){
disease+=temp[r];
}
if(temp[r].contains("紋枯病")==true){
disease+=temp[r];
}
if(temp[r].contains("黃花葉病")==true){
disease+=temp[r];
}
if(temp[r].contains("根腐病")==true){
disease+=temp[r];
}
if(temp[r].contains("稈銹病")==true){
disease+=temp[r];
}
if(temp[r].contains("株期感病")==true){
disease+=temp[r];
}
if(temp[r].contains("株期抗病")==true){
disease+=temp[r];
}
if(temp[r].contains("抗赤黴")==true){
disease+=temp[r];
}
if(temp[r].contains("抗赤黴")==true){
disease+=temp[r];
}
if(true){
col141.setCellValue(disease);
//System.out.println(disease);
}



// if (temp[r].contains("抗病性2")==true) {
// col142.setCellValue(temp[r]);
// }


/**
* 導出txt文件
*/
// out.write("屬性: "+temp[r]+"\r\n");

//System.out.println(temp[r]);//輸出字符串
}
}
}
//System.out.println(list.get(1));
}

// 第五步,將文件存到指定位置
FileOutputStream fout = new FileOutputStream("F://m//2.xls");
wb.write(fout);
fout.close();
/**
* 關閉txt流
*/
// out.flush(); // 把緩存區內容壓入文件
// out.close(); // 最後記得關閉文件
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

4.做成效果如下圖所示

技術分享圖片

技術分享圖片

歡迎各位大佬前來交流+QQ:164368701

java導入excle表格,並且對表格進行相應的修改,並對表格數據進行整理,最後導出本地表格等一系列操作