jxl 修改Excel

package com.wanglin.jxl.operateExcel;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;

import jxl.Cell;
import jxl.CellType;
import jxl.Workbook;
import jxl.write.Number;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.VerticalAlignment;
import jxl.write.DateFormat;
import jxl.write.DateTime;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class CopyExcel {

private static final String PATH = "D:/TestCreateExcel.xls";

/**
* 复制就是复制一样的.但也是可以修改,就是按这种方法修改
*/
public static void copyCreateExcel() {
Workbook wb = null;
WritableWorkbook wwb = null;
try {
//创建只读的Excel工作薄的对象
wb = Workbook.getWorkbook(new File(PATH));
//创建可写入的Excel工作薄对象
wwb = Workbook.createWorkbook(new File(PATH),wb);
WritableSheet sheet = wwb.getSheet(0);
// Label
WritableCell cell = sheet.getWritableCell(0, 0);
// Number
Cell cell2 = sheet.getWritableCell(1, 0);
// DateTime
Cell cell3 = sheet.getWritableCell(2, 0);
// 空
Cell cell4 = sheet.getWritableCell(4,0);
// Formula
Cell cell5 = sheet.getWritableCell(5,0);
// WritableHyperlink
Cell cell6 = sheet.getWritableCell(6,0);


// 设置左对齐红色单元格
WritableFont writableFont = new WritableFont(WritableFont.ARIAL,20,WritableFont.BOLD);
writableFont.setColour(Colour.RED);
WritableCellFormat cellFormat = new WritableCellFormat(writableFont);
cellFormat.setAlignment(Alignment.LEFT);
cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);

// 设置居中绿色单元格,黄色双边框
WritableFont writableFont2 = new WritableFont(WritableFont.ARIAL,20,WritableFont.NO_BOLD);
writableFont2.setColour(Colour.GREEN);
WritableCellFormat cellFormat2 = new WritableCellFormat(writableFont2);
cellFormat2.setAlignment(Alignment.CENTRE);
cellFormat2.setVerticalAlignment(VerticalAlignment.CENTRE);
cellFormat2.setBorder(Border.ALL, BorderLineStyle.DOUBLE,Colour.YELLOW);

DateFormat dateFormat = new DateFormat("yyyy-MM-dd hh:mm:ss");
WritableFont writableFont3 = new WritableFont(WritableFont.ARIAL,20,WritableFont.NO_BOLD);
writableFont2.setColour(Colour.BROWN);
WritableCellFormat cellFormat3 = new WritableCellFormat(writableFont3,dateFormat);
cellFormat2.setAlignment(Alignment.CENTRE);
cellFormat2.setVerticalAlignment(VerticalAlignment.CENTRE);

if(cell.getType() == CellType.LABEL) {
Label label = (Label)cell;
label.setString("单元格修改成功");
label.setCellFormat(cellFormat);
}
if(cell2.getType() == CellType.NUMBER) {
Number number = (Number)cell2;
number.setValue(8);
number.setCellFormat(cellFormat2);
}
if(cell3.getType() == CellType.DATE) {
DateTime dateTime = (DateTime)cell3;
dateTime.setDate(Calendar.getInstance().getTime());
dateTime.setCellFormat(cellFormat3);
}

// 单元格无值的判断
if(cell4.getType().toString().equalsIgnoreCase("Empty")) {
Number number = new Number(4, 0, 9.2, cellFormat2);
                sheet.addCell(number);
}
// 判断是不是数字公式,如求和,求积等是数字公式
if(cell5.getType() == CellType.NUMBER_FORMULA) {
// FormulaCell formula = (FormulaCell)cell5; //必须转化为FormulaCell
// formula.getFormula(); // 得到公式
// 把公式转化为求积
Formula formula2 = new Formula(5, 0, "PRODUCT(B1,E1)");
sheet.addCell(formula2);
}


} catch (IOException ioe) {
ioe.printStackTrace();
} catch(WriteException we) {
we.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
wwb.write();
wwb.close();
wb.close();
} catch (Exception e) {
e.printStackTrace();
}
}

}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub

CopyExcel.copyCreateExcel();

}
}

你可能感兴趣的:(工作,Excel)