我是靠谱客的博主 威武小蜜蜂,这篇文章主要介绍读取TXT文件内容,生成Excel文件,现在分享给大家,希望可以做个参考。

复制代码
1
需要用到jar文件:poi-3.0.1.jar
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package office; /** * 解析txt文件,输出到Excel文件 * @author JavaAlpha * @date 2011-7-28 * @version V 1.0 */ import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFRow; import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; public class WordReader { /** * @param args */ public static void main(String[] args) { readFileByLine("E:/1.txt"); } /** * 以行为单位读取文件(文本文件) * * @param filePath */ public static void readFileByLine(String filePath) { File file = new File(filePath); BufferedReader bd = null; Map<String, String> str = new HashMap<String, String>(); String s1 = ""; String s2 = ""; try { bd = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gb2312"));// 编码转换(关键的地方) String temp = ""; int line = 1; while ((temp = bd.readLine()) != null) { if (temp.length() > 0) { s1 = temp.substring(0, 3); s1 = s1.trim(); s2 = temp.substring(4); s2 = s2.trim(); str.put(s1, s2); } ++line; } createExcel(str); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { if (bd != null) bd.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 输出Excel文件,输出格式为多行两列 * @param map */ static void createExcel(Map<String, String> map) { try { // 新建一输出文件流 FileOutputStream fOut = new FileOutputStream("e:/2.xls"); File file = new File("e:/2.xls"); if (file.exists()) { file.delete(); } // 创建新的Excel 工作簿 HSSFWorkbook workbook = new HSSFWorkbook(); // 在Excel工作簿中建一工作表,其名为缺省值 // 如要新建一名为"联系人用户名和电话"的工作表,其语句为: HSSFSheet sheet = workbook.createSheet("联系人用户名和电话"); HSSFRow row = null; // 在索引0的位置创建单元格(左上端) HSSFCell cell1 = null; HSSFCell cell2 = null; Iterator iter = map.entrySet().iterator(); int i = 0; while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); Object key = entry.getKey(); Object val = entry.getValue(); row = sheet.createRow((short) i++); cell1 = row.createCell((short) 0); cell2 = row.createCell((short) 1); // 定义单元格为字符串类型 cell1.setCellType(HSSFCell.CELL_TYPE_STRING); cell2.setCellType(HSSFCell.CELL_TYPE_STRING); // 在单元格中输入一些内容 cell1.setCellValue(key.toString()); cell2.setCellValue(val.toString()); if (i > 255) { break; } } // 把相应的Excel 工作簿存盘 workbook.write(fOut); fOut.flush(); // 操作结束,关闭文件 fOut.close(); System.out.println("文件生成..."); } catch (Exception e) { System.out.println("出现异常: " + e); } } }

最后

以上就是威武小蜜蜂最近收集整理的关于读取TXT文件内容,生成Excel文件的全部内容,更多相关读取TXT文件内容内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(80)

评论列表共有 0 条评论

立即
投稿
返回
顶部