码迷,mamicode.com
首页 > 编程语言 > 详细

java导入excle表格,并且对表格进行相应的修改,并对表格数据进行整理,最后导出本地表格等一系列操作

时间:2018-11-09 13:52:25      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:static   auto   inf   读取excel   ade   特性   本地   val   品种   

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表格,并且对表格进行相应的修改,并对表格数据进行整理,最后导出本地表格等一系列操作

标签:static   auto   inf   读取excel   ade   特性   本地   val   品种   

原文地址:https://www.cnblogs.com/xt-club/p/9934487.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!