-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathReadExample.java
More file actions
executable file
·49 lines (44 loc) · 1.42 KB
/
ReadExample.java
File metadata and controls
executable file
·49 lines (44 loc) · 1.42 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
import jxl.*;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import java.io.*;
public class ReadExample {
public static void main(String[] args){
try{
String pathToFile="d:\\temp.xls";
write(pathToFile);
read(pathToFile);
}catch(Exception ex){
System.err.println("ReadExample Exception:"+ex.getMessage());
}
}
private static void write(String pathToFile) throws Exception{
// settings for workbook
WorkbookSettings settings=new WorkbookSettings();
settings.setEncoding("WINDOWS-1251");
// create and open workbook
WritableWorkbook workbook=Workbook.createWorkbook(new File(pathToFile),settings);
// create sheet into workbook
WritableSheet sheet=workbook.createSheet("ëèñò îäèí", 0);
// cell format
CellView cellView=new CellView();
cellView.setSize(10000);
// set cell format
sheet.setColumnView(0, cellView);
// add cell into sheet
sheet.addCell(new Label(0,0,"ýòî âðåìåííîå çíà÷åíèå"));
// write workbook
workbook.write();
workbook.close();
}
private static void read(String pathToFile) throws Exception{
Workbook workbook=Workbook.getWorkbook(new File(pathToFile));
Sheet sheet=workbook.getSheet(0);
Cell[] columnOne=sheet.getColumn(0);
for(int counter=0;counter<columnOne.length;counter++){
System.out.println(sheet.getCell(0,counter).getContents());
}
workbook.close();
}
}