我是靠谱客的博主 谦让方盒,这篇文章主要介绍Java 向Excle表中指定行插入数据、读取表中内容、创建excle表,现在分享给大家,希望可以做个参考。

Java  向已知Excle表累计追加数据

1、创建新表

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public static void creatExcel(String filePath,String barCode, String qrCode,String state) throws Exception { File file = new File(filePath); // 当文件夹不存在时,创建文件 if (!file.exists()) { file.createNewFile(); // 创建sheet表,数据 createSheet(barCode,qrCode, file,state); }else { //1、判断是否为空表,若是空表,则重新创建表,并写入数据 //2、获取表行数,来判断有没有实际数据 int rowNumber = getLastRowNumber(filePath); // 1:表示头行标题,不是正式值 if (rowNumber >=1) { //2、表数据已存在 向已知表中插入数据 --------------- inserSheetData(filePath,barCode,qrCode,state); }else { // 空表,重新创建sheet表 createSheet(barCode,qrCode,file,state); } } }

2、表已知,并且有数据 插入数据

复制代码
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
import java.io.FileInputStream; import java.io.FileOutputStream; 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; public class test{ public static void main(String[] args) throws Exception{ inserSheetData(); } //向已知表中插入数据,累计追加 //写入前先判断表是否存在,表中是否有数据 private static void inserSheetData() throws Exception { String filePath = "D:\Printer\" + "Printer"+ ".xls"; FileInputStream fs=new FileInputStream(filePath); //使用POI提供的方法得到excel的信息 POIFSFileSystem fileSystem = new POIFSFileSystem(fs); HSSFWorkbook hssfWorkbook = new HSSFWorkbook(fileSystem); //获取到工作表,因为一个excel可能有多个工作表 HSSFSheet InsertSheet=hssfWorkbook.getSheetAt(0); //获取第一行(excel中的行默认从0开始,所以这就是为什么,一个excel必须有字段列头),即,字段列头,便于赋值 HSSFRow row=InsertSheet.getRow(0); //分别得到最后一行的行号,和一条记录的最后一个单元格 System.out.println("最后一行的行 "+InsertSheet.getLastRowNum()); //向文件中写入数据 FileOutputStream out=new FileOutputStream(filePath); int lastRowNum = InsertSheet.getLastRowNum()+1; //在指定行后追加数据 row=InsertSheet.createRow((short)(lastRowNum)); //设置第一个(从0开始)单元格的数据 row.createCell(0).setCellValue("xiaoming"); row.createCell(1).setCellValue(24); row.createCell(2).setCellValue("nan"); row.createCell(3).setCellValue("nanjing"); out.flush(); hssfWorkbook.write(out); out.close(); System.out.println("物理列数="+row.getPhysicalNumberOfCells()+" 实际列数="+row.getLastCellNum()); } }

3、获取表数据 总行数

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//获取表中最后一行 总行数 @SuppressWarnings("resource") private static int getLastRowNumber(String filePath) throws Exception { HSSFWorkbook hssfWorkbook = new HSSFWorkbook(new FileInputStream(filePath)); HSSFSheet sheet = hssfWorkbook.getSheetAt(0); //加上 头行 int lastRowNum = sheet.getLastRowNum()+1; System.out.println("写入之前的总行数="+lastRowNum); return lastRowNum; }

4、读取 excle文件,指定页内容

复制代码
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
/** * 读取指定Excel文件和sheet页 * * @param filePath * Excel文件全路径 String filePath = "D:\Printer\" + "Printer"+ ".xls"; * @param sheetName * sheet页名称 "sheet-test-1" */ public static ConcurrentHashMap<String, String> readExcel(String filePath,String sheetName) { ConcurrentHashMap<String, String> hashMap = new ConcurrentHashMap<String, String>(); try { // 获取文件源 FileInputStream fis = new FileInputStream(filePath); POIFSFileSystem poifsFileSystem = new POIFSFileSystem(fis);// 可有可无 // 创建对Excel工作簿文件的引用 HSSFWorkbook workbook = new HSSFWorkbook(poifsFileSystem); // 循环读取 HSSFSheet sheet = workbook.getSheet(sheetName); // 获取第一行的内容 HSSFRow row = null; // 遍历行数 for (int m = 0; sheet.getRow(m) != null; m++) { row = sheet.getRow(m); // 遍历当前列 row.getCell(i)!=null for (int i = 0; i < 4; i++) { if (row.getCell(i) != null || !"".equals(row.getCell(i))) { hashMap.put(m + "", row.getCell(i).toString()); System.out.println(m + "行 " + i + "列 " + row.getCell(i)); } } } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return hashMap; }

5、创建excle表

复制代码
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
/** * 点击“录入”时,先生成一张Excel,只包含表头,该Excel添加一行测试信息 * @param timeStamp * @param simNumber * @param serialNumber * @param markContent * @throws Exception */ public static void creatExcel(String timeStamp,String simNumber,String serialNumber,String markContent) throws Exception { // excel的第一行 头 String[] excelHeader = { "单台号", "SIM卡号", "DeviceID", "二维码内容" }; File file = new File(filePath); // 当文件夹不存在时,创建文件 if (!file.exists()) { file.createNewFile(); /* 创建表头 */ // HSSFworkbook表示一个完整的excel表格 HSSFWorkbook workbook = new HSSFWorkbook(); // HSSFsheet表示excel中的一个工作薄,其名为sheet-test-1 HSSFSheet sheet = workbook.createSheet("sheet-test-1"); // HSSFRow表示工作薄中的一行 // 创建一行,参数 表示 0:第一行,1:第二行 HSSFRow row0 = sheet.createRow(0); // HSSFCell表示一个单元格 for (int i = 0; i < excelHeader.length; i++) { // 创建一个单元格,参数 0:第一列,1:第二列 HSSFCell cell = row0.createCell(i); // 赋值 cell.setCellValue(excelHeader[i]); } //填写录入信息 写入第一行 HSSFRow row1 = sheet.createRow(1); HSSFCell cell_1_0 = row1.createCell(0); cell_1_0.setCellValue(timeStamp); HSSFCell cell_1_1 = row1.createCell(1); cell_1_1.setCellValue(simNumber); HSSFCell cell_1_2 = row1.createCell(2); cell_1_2.setCellValue(serialNumber); HSSFCell cell_1_3 = row1.createCell(3); cell_1_3.setCellValue(markContent); // 追加 FileOutputStream os = new FileOutputStream(file); workbook.write(os); os.flush(); os.close(); int lastRowNum = sheet.getLastRowNum()+1; System.out.println("写入完成"+"总行数="+lastRowNum); }else { //1、表已存在,但表中无数据 //读取文件数据,判断有没有实际数据,没有则创建sheet表,再插入数据 ConcurrentHashMap<String,String> readExcel = readExcel(filePath,"sheet-test-1"); //2、判断返回数据是否有实际值 //3、表数据已存在 向已知表中插入数据 --------------- inserSheetData(timeStamp,simNumber,serialNumber,markContent); } }

6、读取指定列数据

复制代码
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
/** * import jxl.Sheet; * import jxl.Workbook; * * * 读取excel表格中特定的 列数据 * * @param filePath * 文件路径 * @param index * 第几列(0开始) * @throws Exception */ public static List readColumn(String filePath, int index) throws Exception { List list = new ArrayList(); InputStream inputStream = new FileInputStream(new File(filePath)); Workbook workbook = Workbook.getWorkbook(inputStream); Sheet sheet = workbook.getSheet(0); int rows = sheet.getRows(); for (int i = 1; i < rows; i++) { //原数据 jxl.Cell cell = sheet.getCell(index, i); String originalData = cell.getContents(); //去除所有空格 String replaceAll = originalData.replaceAll(" +",""); list.add(replaceAll); } return list; }

7、读取指定单元格数据

复制代码
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
/** * * 读取excel表格中特定的 单元格数据 * * @param filePath * 文件路径 * @param column * 第几列(0开始) * @param line * 第几行(0开始) * @throws Exception */ public static String readData(String filePath, int column,int line) throws Exception { String replaceAll=""; InputStream inputStream = new FileInputStream(new File(filePath)); Workbook workbook = Workbook.getWorkbook(inputStream); Sheet sheet = workbook.getSheet(0); // 几列 几行 jxl.Cell cell = sheet.getCell(column, line); String originalData = cell.getContents(); //去除所有空格 replaceAll = originalData.replaceAll(" +",""); //返回数据集合 return replaceAll; } 二、 /** * * 读取excel表格指定 单元格数据 * * @param filePath * 文件路径 * @param colum * 第几列(0开始) * @param line * 第几行(0开始) * @throws Exception */ public static String readTheCellData(String filePath, int colum,int line) throws Exception { String replaceAll=""; InputStream inputStream = new FileInputStream(new File(filePath)); Workbook workbook = Workbook.getWorkbook(inputStream); //读取指定 sheet 表 Sheet sheet = workbook.getSheet(0); //获取表中 共有多少列 int rows = sheet.getRows(); int columns = sheet.getColumns(); // 14 列 System.out.println("共有="+rows +"行,"+columns+"列"); // 防止索引越界,获取最大列数 -1 (指定列13 标记位,列数从0开始) jxl.Cell cell = sheet.getCell(columns-1, line); // 读取指定列 13 (通过、未通过) // jxl.Cell cell = sheet.getCell(colum, line); //读取指定单元格内容 String originalData = cell.getContents(); //去除所有空格 replaceAll = originalData.replaceAll(" +",""); //返回数据集合 return replaceAll; }
复制代码
1
2
3
4
5
6
//文件存储路径 String saveDataPath = "D:\Printer\" + "Printer"+ ".xls"; String readData = readData(saveDataPath, 10,0); System.out.println("获取指定单元格数据="+readData);

最后

以上就是谦让方盒最近收集整理的关于Java 向Excle表中指定行插入数据、读取表中内容、创建excle表的全部内容,更多相关Java内容请搜索靠谱客的其他文章。

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

评论列表共有 0 条评论

立即
投稿
返回
顶部