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

     2022-04-20     302

关键词:

1.首先创建一个java项目

  完成效果如下图所示

2.导入以下jar包

3.代码如下

  其中行和列的操作是根据需求自动划分的

复制代码
1 public class auto_date {
2 private static List<List<String>> readExcel(File file) throws Exception {
3 // 创建输入流,读取Excel
4 InputStream is = new FileInputStream(file.getAbsolutePath());
5 // jxl提供的Workbook类
6 Workbook wb = Workbook.getWorkbook(is);
7 // 只有一个sheet,直接处理
8 //创建一个Sheet对象
9 Sheet sheet = wb.getSheet(0);
10 // 得到所有的行数
11 int rows = sheet.getRows();
12 // 所有的数据
13 List<List<String>> allData = new ArrayList<List<String>>();
14 // 越过第一行 它是列名称
15 for (int j = 1; j < rows; j++) {
16 List<String> oneData = new ArrayList<String>();
17 // 得到每一行的单元格的数据
18 Cell[] cells = sheet.getRow(j);
19 for (int k = 0; k < cells.length; k++) {
20 oneData.add(cells[k].getContents().trim());
21 }
22 // 存储每一条数据
23 allData.add(oneData);
24 // 打印出每一条数据
25 //System.out.println(oneData);
26 }
27 return allData;
28 }
29 public static void main(String[] args) {
30 File file = new File("F://m//1.xls");
31 //42列
32 //3337行
33 try {
34 List<List<String>> allData=readExcel(file);
35 //System.out.println("总数:"+allData.size());//总行数
36 /*
37
创建excle表格
38 */
39 // 第一步,创建一个webbook,对应一个Excel文件
40 HSSFWorkbook wb = new HSSFWorkbook();
41 // 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
42 HSSFSheet sheet = wb.createSheet("小麦特性表");
43 // 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
44 HSSFRow row = sheet.createRow((int) 0);
45 // 第四步,创建单元格,并设置值表头 设置表头居中
46 //HSSFCellStyle style = wb.createCellStyle();
47 //style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
48 // HSSFRow row1 = sheet.createRow(0);
49 HSSFCell cell = row.createCell((short) 0);
50 cell.setCellValue("所有小麦特征表");
51 sheet.addMergedRegion(new CellRangeAddress(0,0,0,20));
52 HSSFRow row2 = sheet.createRow(1);
53 row2.createCell(0).setCellValue("品种名称");
54 row2.createCell(1).setCellValue("生态类型");
55 row2.createCell(2).setCellValue("生育期");
56 row2.createCell(3).setCellValue("苗性");
57 row2.createCell(4).setCellValue("叶色");
58 row2.createCell(5).setCellValue("分蘖力");
59 row2.createCell(6).setCellValue("株型");
60 row2.createCell(7).setCellValue("株高");
61 row2.createCell(8).setCellValue("株高");
62 row2.createCell(9).setCellValue("穗形");
63 row2.createCell(10).setCellValue("芒");
64 row2.createCell(11).setCellValue("壳色");
65 row2.createCell(12).setCellValue("粒色");
66 row2.createCell(13).setCellValue("硬度");
67 row2.createCell(14).setCellValue("籽粒饱满度");
68 row2.createCell(15).setCellValue("亩穗数");
69 row2.createCell(16).setCellValue("穗粒数");
70 row2.createCell(17).setCellValue("千粒重");
71 row2.createCell(18).setCellValue("熟相");
72 row2.createCell(19).setCellValue("抗倒性");
73 row2.createCell(20).setCellValue("抗旱性");
74 row2.createCell(21).setCellValue("抗寒性1");
75 row2.createCell(22).setCellValue("抗寒性2");
76 row2.createCell(23).setCellValue("粗蛋白质");
77 row2.createCell(24).setCellValue("湿面筋");
78 row2.createCell(25).setCellValue("降落数值");
79 row2.createCell(26).setCellValue("沉淀指数");
80 row2.createCell(27).setCellValue("吸水量");
81 row2.createCell(28).setCellValue("形成时间");
82 row2.createCell(29).setCellValue("稳定时间");
83 row2.createCell(30).setCellValue("弱化度");
84 row2.createCell(31).setCellValue("出粉率");
85 row2.createCell(32).setCellValue("延伸性");
86 row2.createCell(33).setCellValue("拉伸能量");
87 row2.createCell(34).setCellValue("拉伸面积");
88 row2.createCell(35).setCellValue("最大拉伸阻力");
89 row2.createCell(36).setCellValue("容重");
90 row2.createCell(37).setCellValue("节水性指数1");
91 row2.createCell(38).setCellValue("节水性指数2");
92 row2.createCell(39).setCellValue("节水性1");
93 row2.createCell(40).setCellValue("节水性2");
94 row2.createCell(41).setCellValue("抗病性1");
95 row2.createCell(42).setCellValue("抗病性2");
96
97
98
99
100
101 /
102 导出txt文件
103
/
104 // File writename = new File("G://2.txt"); // 相对路径,如果没有则要建立一个新的output。txt文件
105 // writename.createNewFile(); // 创建新文件
106 // BufferedWriter out = new BufferedWriter(new FileWriter(writename));
107 // out.write("写入文件 "); // 即为换行
108
109
110
111 //System.out.println(allData);//输出全部
112 for (int i = 0; i < allData.size(); i++) {
113 List<String> list=allData.get(i);
114 HSSFRow row3 = sheet.createRow(i+2);
115 HSSFCell col0 = row3.createCell(0);
116 col0.setCellValue(list.get(0));
117 /

118 导出txt文件
119
/
120 // out.write("**"+" ");
121 // out.write("第 "+i+" 个"+"品种名称: "+list.get(0)+" ");
122 //*****
123 //System.out.println(allData.get(i));//逐条输出
124 //System.out.println(list.get(0));
125 for (int j = 0; j <list.size(); j++) {
126 String str=list.get(j);
127
128 String newstr=str.replace(",", "。");
129 String newstr1=newstr.replace(";","。");
130 String newstr2=newstr1.replace("、","。");
131 String newstr3=newstr2.replace(";","。");
132 String newstr4=newstr3.replace(",","。");
133 //System.out.println(newstr1);
134 String[] temp;
135 String delimeter = "。"; // 指定分割字符
136 temp = newstr4.split(delimeter); // 分割字符串
137 // 普通 for 循环
138 for(int q =0; q < temp.length ; q++){
139 String disease="";
140 //生态类型
141 HSSFCell col11 = row3.createCell(1);
142 //生育周期
143 HSSFCell col12 = row3.createCell(2);
144 //苗性
145 HSSFCell col13 = row3.createCell(3);
146 //叶色
147 HSSFCell col14 = row3.createCell(4);
148 //分蘖力
149 HSSFCell col15 = row3.createCell(5);
150 //穗形
151 HSSFCell col16 = row3.createCell(6);
152 //芒
153 HSSFCell col17 = row3.createCell(7);
154 //壳色
155 HSSFCell col18 = row3.createCell(8);
156 //芒
157 HSSFCell col19 = row3.createCell(9);
158 //壳
159 HSSFCell col110 = row3.createCell(10);
160 //粒色
161 HSSFCell col111 = row3.createCell(11);
162 //硬度
163 HSSFCell col112 = row3.createCell(12);
164 //籽粒饱满度
165 HSSFCell col113 = row3.createCell(13);
166 //亩穗数
167 HSSFCell col114 = row3.createCell(14);
168 //穗粒数
169 HSSFCell col115 = row3.createCell(15);
170 //千粒重
171 HSSFCell col116 = row3.createCell(16);
172 //熟相
173 HSSFCell col117 = row3.createCell(17);
174 //抗倒性
175 HSSFCell col118 = row3.createCell(18);
176 //抗旱性
177 HSSFCell col119 = row3.createCell(19);
178 //抗寒性
179 HSSFCell col120 = row3.createCell(20);
180 HSSFCell col121 = row3.createCell(21);
181 HSSFCell col122 = row3.createCell(22);
182 HSSFCell col123 = row3.createCell(23);
183 HSSFCell col124 = row3.createCell(24);
184 HSSFCell col125 = row3.createCell(25);
185 HSSFCell col126 = row3.createCell(26);
186 HSSFCell col127 = row3.createCell(27);
187 HSSFCell col128 = row3.createCell(28);
188 HSSFCell col129 = row3.createCell(29);
189 HSSFCell col130 = row3.createCell(30);
190 HSSFCell col131 = row3.createCell(31);
191 HSSFCell col132 = row3.createCell(32);
192 HSSFCell col133 = row3.createCell(33);
193 HSSFCell col134 = row3.createCell(34);
194 HSSFCell col135 = row3.createCell(35);
195 HSSFCell col136 = row3.createCell(36);
196 HSSFCell col137 = row3.createCell(37);
197 HSSFCell col138 = row3.createCell(38);
198 HSSFCell col139 = row3.createCell(39);
199 HSSFCell col140 = row3.createCell(40);
200 HSSFCell col141 = row3.createCell(41);
201 HSSFCell col142 = row3.createCell(42);
202 HSSFCell col143 = row3.createCell(43);
203 for(int r =0; r < temp.length ; r++){
204 if (temp[r].contains("春性")==true||temp[r].contains("冬性")==true) {
205
206 col11.setCellValue(temp[r]);
207 }
208 if (temp[r].contains("生育期")==true) {
209 col12.setCellValue(temp[r]);
210 }
211 if (temp[r].contains("幼苗")==true) {
212 col13.setCellValue(temp[r]);
213 }
214 if (temp[r].contains("叶色")==true) {
215 col14.setCellValue(temp[r]);
216 }
217 if (temp[r].contains("分蘖力")==true) {
218 col15.setCellValue(temp[r]);
219 }
220 if (temp[r].contains("株型")==true) {
221 col16.setCellValue(temp[r]);
222 }
223 if (temp[r].contains("株高")==true) {
224 col17.setCellValue(temp[r]);
225 }
226 if (temp[r].contains("穗纺锤")==true||temp[r].contains("穗长方")==true||temp[r].contains("穗棍棒")==true
227 ||temp[r].contains("纺锤型")==true||temp[r].contains("棍棒形")==true
228 ||temp[r].contains("穗型长方形")==true||temp[r].contains("长方穗型")==true) {
229 col19.setCellValue(temp[r]);
230 }
231 if (temp[r].contains("芒")==true) {
232 col110.setCellValue(temp[r]);
233 }
234 if (temp[r].contains("壳")==true) {
235 col111.setCellValue(temp[r]);
236 }
237 if (temp[r].contains("红粒")==true||temp[r].contains("蓝粒")==true||
238 temp[r].contains("白粒")==true||temp[r].contains("黑粒")==true||temp[r].contains("粒红")==true
239 ||temp[r].contains("粒黑")==true||temp[r].contains("粒白")==true) {
240 col112.setCellValue(temp[r]);
241 }
242 if (temp[r].contains("硬质")==true) {
243 col113.setCellValue(temp[r]);
244 }
245 if (temp[r].contains("饱满")==true) {
246 col114.setCellValue(temp[r]);
247 }
248 if (temp[r].contains("亩穗数")==true) {
249 col115.setCellValue(temp[r]);
250 }
251 if (temp[r].contains("穗粒数")==true) {
252 col116.setCellValue(temp[r]);
253 }
254 if (temp[r].contains("千粒重")==true) {
255 col117.setCellValue(temp[r]);
256 }
257 if (temp[r].contains("熟相")==true) {
258 col118.setCellValue(temp[r]);
259 }
260 if (temp[r].contains("抗倒性")==true) {
261 col119.setCellValue(temp[r]);
262 }
263 if (temp[r].contains("抗旱性")==true) {
264 col120.setCellValue(temp[r]);
265 }
266 if (temp[r].contains("抗寒性")==true) {
267 col121.setCellValue(temp[r]);
268 }
269 // if (temp[r].contains("抗寒性")==true) {
270 // col122.setCellValue(temp[r]);
271 // }
272 if (temp[r].contains("粗蛋白质")==true) {
273 col123.setCellValue(temp[r]);
274 }
275 if (temp[r].contains("湿面筋")==true) {
276 col124.setCellValue(temp[r]);
277 }
278 if (temp[r].contains("降落数值")==true) {
279 col125.setCellValue(temp[r]);
280 }
281 if (temp[r].contains("沉淀指数")==true) {
282 col126.setCellValue(temp[r]);
283 }
284 if (temp[r].contains("吸水量")==true||temp[r].contains("吸水率")==true) {
285 col127.setCellValue(temp[r]);
286 }
287 if (temp[r].contains("形成时间")==true) {
288 col128.setCellValue(temp[r]);
289 }
290 if (temp[r].contains("稳定时间")==true) {
291 col129.setCellValue(temp[r]);
292 }
293 if (temp[r].contains("弱化度")==true) {
294 col130.setCellValue(temp[r]);
295 }
296 if (temp[r].contains("出粉率")==true) {
297 col131.setCellValue(temp[r]);
298 }
299 if (temp[r].contains("延伸性")==true) {
300 col132.setCellValue(temp[r]);
301 }
302 if (temp[r].contains("拉伸能量")==true) {
303 col133.setCellValue(temp[r]);
304 }
305 if (temp[r].contains("拉伸面积")==true) {
306 col134.setCellValue(temp[r]);
307 }
308 if (temp[r].contains("最大拉伸阻力")==true) {
309 col135.setCellValue(temp[r]);
310 }
311 if (temp[r].contains("容重")==true) {
312 col136.setCellValue(temp[r]);
313 }
314 if (temp[r].contains("节水指数")==true) {
315 col137.setCellValue(temp[r]);
316 }
317 // if (temp[r].contains("节水指数2")==true) {
318 // col138.setCellValue(temp[r]);
319 // }
320 if (temp[r].contains("节水性")==true) {
321 col139.setCellValue(temp[r]);
322 }
323 // if (temp[r].contains("节水性2")==true) {
324 // col140.setCellValue(temp[r]);
325 // }
326 /
327 各种病症
328
/
329 if(temp[r].contains("抗病鉴定")==true){
330 disease+=temp[r]+":";
331 }
332 if(temp[r].contains("叶锈病")==true){
333 disease+=temp[r]+",";
334 }
335 if(temp[r].contains("条锈病")==true){
336 disease+=temp[r]+",";
337 }
338 if(temp[r].contains("×××病")==true){
339 disease+=temp[r]+",";
340 }
341 if(temp[r].contains("赤霉病")==true){
342 disease+=temp[r]+",";
343 }
344 if(temp[r].contains("抗条锈病")==true){
345 disease+=temp[r];
346 }
347 if(temp[r].contains("纹枯病")==true){
348 disease+=temp[r];
349 }
350 if(temp[r].contains("黄花叶病")==true){
351 disease+=temp[r];
352 }
353 if(temp[r].contains("根腐病")==true){
354 disease+=temp[r];
355 }
356 if(temp[r].contains("秆锈病")==true){
357 disease+=temp[r];
358 }
359 if(temp[r].contains("株期感病")==true){
360 disease+=temp[r];
361 }
362 if(temp[r].contains("株期抗病")==true){
363 disease+=temp[r];
364 }
365 if(temp[r].contains("抗赤霉")==true){
366 disease+=temp[r];
367 }
368 if(temp[r].contains("抗赤霉")==true){
369 disease+=temp[r];
370 }
371 if(true){
372 col141.setCellValue(disease);
373 //System.out.println(disease);
374 }
375
376
377
378 // if (temp[r].contains("抗病性2")==true) {
379 // col142.setCellValue(temp[r]);
380 // }
381
382
383 /

384 导出txt文件
385
/
386 // out.write("属性: "+temp[r]+" ");
387
388 //System.out.println(temp[r]);//输出字符串
389 }
390 }
391 }
392 //System.out.println(list.get(1));
393 }
394
395
396
397 // 第五步,将文件存到指定位置
398 FileOutputStream fout = new FileOutputStream("F://m//2.xls");
399 wb.write(fout);
400 fout.close();
401 /*
402
关闭txt流
403 */
404 // out.flush(); // 把缓存区内容压入文件
405 // out.close(); // 最后记得关闭文件
406 } catch (Exception e) {
407 // TODO Auto-generated catch block
408 e.printStackTrace();
409 }
410 }
411 }
复制代码

  

4.做成效果如下图所示

java通过poi对表格文件写入数据

一、下载并导入相应版本的包(根据Java版本)下载地址:https://archive.apache.org/dist/poi/release/bin/需要导入的包有:  压缩文件根目录下的5个包,lib文件下的3个包二、新建表格publicstaticvoidmain(String[]args)throwsIOException{Workbookwb=... 查看详情

c#excl表格的导入功能(代码片段)

1,添加NOPI的引用下载地址:http://download-codeplex.sec.s-msft.com/Download/Release?ProjectName=npoi&DownloadId=1432518&FileTime=130691232697500000&Build=210662,添加命名空间usingNPOI.HSSF.UserModel;usingNP 查看详情

java设置word表格边框

概述本文介绍通过Java程序设置Word表格边框的方法,设置边框时可对整个表格设置,也可对指定单元格设置,同时可对边框进行格式化设置设置,包括边框类型、样式、颜色、线条宽度等等,下面将分三个示例来展示如何设置边... 查看详情

java示例代码_JXTable-如何更新highlighters对表格进行排序

java示例代码_JXTable-如何更新highlighters对表格进行排序 查看详情

如何实现在网络上由各单位填写excle表格以方便汇总。

...款报表统计汇总软件能实现你说的功能,下属单位录入或导入数据后总公司直接可以汇总,还可以进行季度、半年、全年的统计汇总。支持复杂格式的表头和表尾,这是用过的样表可以参考下:参考技术A如果单用户名密码,大... 查看详情

使用jquery对表格的行进行增删。要求:用按钮对表格的行进行增删,并且增加的行数不能超过5行。。

  $(document).ready(function()      //增加一行      vari=1;      $("#add").click(function()      if(i<5)          varidval='tr'+i;&... 查看详情

mysql+navicat导入excel表格内的数据

...数据编辑3.保存MySQL中建立一张表,把Excel表格的数据导入MySQL数据库中。4.创建表数据结构(与excel数据相对应)5.导入向导(鼠标右键表)6.选择导入数据格式7.选择导入excel数据模板,选着sheet页8.选择导入... 查看详情

mysql+navicat导入excel表格内的数据

...数据编辑3.保存MySQL中建立一张表,把Excel表格的数据导入MySQL数据库中。4.创建表数据结构(与excel数据相对应)5.导入向导(鼠标右键表)6.选择导入数据格式7.选择导入excel数据模板,选着sheet页8.选择导入... 查看详情

excle表格生成网页

 用Dreamweaver可以方便生成和excle表格一样的代码。 复制excle,粘贴     查看详情

XSLT 使用来自外部文档的信息对表格进行排序

...遇到了重大问题,我不知道如何进行排序。我正在尝试对导入.XSL的XSLT中的表进行排序。在这个.XSL中,我引用了两个外部.XSL。输出应该是html。mainXSL.xsl<?xmlversion="1 查看详情

vue+xlsx实现表格的导入导出

...大多都会涉及到表格的处理,其中最为常见的就是表格的导入导出。有很多办法都可以实现,其中最简单的还是使用插件xlsx。实现目标1、对表格数据进行增加、删除。2、表格数据的导出、导入。具体逻辑增加、删除功能比较简... 查看详情

在表格视图中显示时对数组进行排序

...用于姓名,另一个用于电话号码,当我将电话联系人姓名导入第一个数组(名称数组)和另一个数组(数字数组)中的适当数字时,问题是当我将这两个数组按排序顺序显示在tableview中。如何对这两个数组进行排序并在tableview中... 查看详情

excel表格里怎么用用公式进行数据引用

如果在一个很大的表格里去用公式筛选,该怎么筛选呢,比如说我现在要筛选产品1,时间是9-12那么怎么用公式直接生成后面的数据呢?,如果我在另外表格里设置了所有产品的名称及价格,那么我在这张表格里输入了产品名后... 查看详情

网页导出无法生成excel,有啥方法可以解决吗?

...LE。选择数据。03然后选择自网站。04在地址栏输入我们要导入的表格所在网站。然后回车进入。05黄色的小箭头代表选定,点击一下。06选定好表格后再点击导入。07然后选择表格的起始位置,点击确定。08成功导入。导入Word新建... 查看详情

sas的导入导出excel表格的实现(代码片段)

首先SAS可以使用手动来导入,导出但是这样对于每次操作都需要来手动操作,所以就使用了SAS中的宏来编写代码需求:1.首先是给定excel的文件路径,来生成一个数据集2.然后是对数据集中进行数据的处理3.最后是对处理好的数据集导出... 查看详情

如何将记事本文档导入到excel表格中?

利用excel的数据设置功能即可将记事本文档导入到excel表格中,具体操作请参照以下步骤。1、在电脑上打开一个excel表格文件,点击上方菜单工具栏中的数据选项,找到“获得外部数据”选项区域。2、在获取外部数据选项卡中选... 查看详情

对表格进行排序时,Vue v-for循环中的重复键

...23:12:36【问题描述】:我的Vue应用程序使用vuefire从Firestore导入数据。我从“行”集合中导入数据,如下所示:firestore()returnlines:db.collection("lines")每条记录都有一个Firestor 查看详情

精准控制表格列表(table-layout:fixed)

现在面临的问题:对于元素的display的属性使用表格的形式,可以使元素有表格的行为,但对于不固定宽度的的元素,进行布局也会很难控制。这是因为列宽会对表格进行相应的调整,即使指定了相应的宽度,也只是能起到类似... 查看详情