-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest2.java
More file actions
64 lines (53 loc) · 1.83 KB
/
Test2.java
File metadata and controls
64 lines (53 loc) · 1.83 KB
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
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;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Test2{
public static void main(String[] args) throws IOException {
Test2 t2 = new Test2();
t2.creatFile();
t2.appendFile();
}
public void creatFile() throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
HSSFRow row = sheet.createRow(2);
HSSFCell cell = row.createCell(3);
cell.setCellValue("我是你爹----by Simon Chen");
Path path = Paths.get("D:\\").resolve("Hello.xls");
if(Files.exists(path)){
Files.delete(path);
}
File file = Files.createFile(path).toFile();
workbook.write(file);
}
public void appendFile() throws IOException{
String path = "D:\\Hello.xls";
File file = new File(path);
int rowIndex = 3;
int columnIndex = 5;
HSSFWorkbook workbook = null;
HSSFSheet sheet = null;
if(file.exists()){
FileInputStream fis = new FileInputStream(file);
workbook = new HSSFWorkbook(fis);
sheet = workbook.getSheet("Sheet0");
}else {
workbook = new HSSFWorkbook();
sheet = workbook.createSheet();
}
HSSFRow row = sheet.getRow(rowIndex);
if(row == null){
row = sheet.createRow(rowIndex);
}
HSSFCell cell = row.createCell(columnIndex);
cell.setCellValue("追加:really---by Peter Mao");
workbook.write(file);
}
}