簡単なjava読み書きexcel
まずExcelを読みます.
次にExcelを書きます.
以上のコードはjxl.jarパッケージをインポートする必要があります.
package August;
import java.io.File;
import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.NumberCell;
import jxl.Sheet;
import jxl.Workbook;
public class ReadExcel {
public static void main(String[] args) {
Workbook workbook = null;
try {
workbook = Workbook.getWorkbook(new File("d:\\readExcel.xls"));
} catch (Exception e) {
e.printStackTrace();
}
Sheet sheet = workbook.getSheet(0);
Cell cell = null;
int columnCount=4;//
int rowCount=sheet.getRows();//
for (int i = 0; i <rowCount; i++) {
for (int j = 0; j <columnCount; j++) {
// , , ,
cell=sheet.getCell(j, i);
// ,
if(cell.getType()==CellType.NUMBER){
System.out.print(((NumberCell)cell).getValue());
}
else if(cell.getType()==CellType.DATE){
System.out.print(((DateCell)cell).getDate());
}
else{
System.out.print(cell.getContents());
}
//System.out.print(cell.getContents());
System.out.print("\t");
}
System.out.print("
");
}
// ,
workbook.close();
}
}
次にExcelを書きます.
package August;
import java.io.File;
import java.io.IOException;
import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.write.DateFormat;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.NumberFormat;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
public class WriteExcel {
public static void main(String[] args) {
File tempFile=new File("d:/writeExcel.xls");
WritableWorkbook workbook = null;
try {
workbook = Workbook.createWorkbook(tempFile);
} catch (IOException e) {
e.printStackTrace();
}
WritableSheet sheet = workbook.createSheet("TestCreateExcel", 0);
// , excel
Label l=null;
jxl.write.Number n=null;
DateTime d=null;
// , Excel
WritableFont headerFont = new WritableFont(WritableFont.ARIAL, 12, WritableFont.BOLD, false,UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLUE);
WritableCellFormat headerFormat = new WritableCellFormat (headerFont);
WritableFont titleFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.RED);
WritableCellFormat titleFormat = new WritableCellFormat (titleFont);
WritableFont detFont = new WritableFont(WritableFont.ARIAL, 10, WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLACK);
WritableCellFormat detFormat = new WritableCellFormat (detFont);
NumberFormat nf=new NumberFormat("0.00000"); // Number
WritableCellFormat priceFormat = new WritableCellFormat (detFont, nf);
DateFormat df=new DateFormat("yyyy-MM-dd");//
WritableCellFormat dateFormat = new WritableCellFormat (detFont, df);
// , , sheet
l=new Label(1, 0, " Excel ", headerFormat);
try{
sheet.addCell(l);
//add Title
int column=0;
l=new Label(column++, 2, " ", titleFormat);
sheet.addCell(l);
l=new Label(column++, 2, " ", titleFormat);
sheet.addCell(l);
l=new Label(column++, 2, " ", titleFormat);
sheet.addCell(l);
l=new Label(column++, 2, " ", titleFormat);
sheet.addCell(l);
//add detail
int i=0;
column=0;
l=new Label(column++, i+3, " "+i, detFormat);
sheet.addCell(l);
d=new DateTime(column++, i+3, new java.util.Date(), dateFormat);
sheet.addCell(d);
l=new Label(column++, i+3, "CNY", detFormat);
sheet.addCell(l);
n=new jxl.write.Number(column++, i+3, 5.678, priceFormat);
sheet.addCell(n);
i++;
column=0;
l=new Label(column++, i+3, " "+i, detFormat);
sheet.addCell(l);
d=new DateTime(column++, i+3, new java.util.Date(), dateFormat);
sheet.addCell(d);
l=new Label(column++, i+3, "SGD", detFormat);
sheet.addCell(l);
n=new jxl.write.Number(column++, i+3, 98832, priceFormat);
sheet.addCell(n);
//
column=0;
sheet.setColumnView(column++, 20);
sheet.setColumnView(column++, 20);
sheet.setColumnView(column++, 10);
sheet.setColumnView(column++, 20);
workbook.write();
workbook.close();
}catch(Exception e) {
}
}
}
以上のコードはjxl.jarパッケージをインポートする必要があります.