Excel更新单元格的内容

来源:互联网 发布:凸优化应用 编辑:程序博客网 时间:2024/05/20 19:32
package com.name.test;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;


import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.VerticalAlignment;
import jxl.read.biff.BiffException;
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;
import jxl.write.biff.RowsExceededException;


import org.apache.struts2.ServletActionContext;


import com.name.pojo.ExtraListen;
import com.name.pojo.ExtraRead;
import com.name.pojo.Listen;
import com.name.pojo.Read;
import com.name.student.dao.ExtraListenDAO;
import com.name.teacher.service.ExtraListenService;
import com.name.teacher.service.ListenService;
import com.name.teacher.service.impl.ExtraListenServiceImpl;
import com.name.teacher.service.impl.ListenServiceImpl;
/*
 * 更新Excel
 */
public class TeacherTest {
public static void main(String args[]) { 
WritableWorkbook book = null;  
    try {  
        // Excel获得文件  
        Workbook wb = Workbook.getWorkbook(new File("d:/EL001.xls"));  
        // 打开一个文件的副本,并且指定数据写回到原文件  
        book = Workbook.createWorkbook(new File("d:/EL001.xls"), wb);  
        Sheet sheet = book.getSheet(0);  
        WritableSheet wsheet = book.getSheet(0);  
        // 不读表头  
        for (int i = 1; i < sheet.getRows(); i++) {  
            String answer = sheet.getCell(1, i).getContents().trim();
            WritableCell cell =((WritableSheet) sheet).getWritableCell(0, 0);//获取第一个单元格
            jxl.format.CellFormat cf = cell.getCellFormat();//获取第一个单元格的格式

            Label label = new Label(1, i, answer.replaceAll("@", "@@@"));

//     Label label = new Label(1, i, answer.replaceAll("@", "@@@"),getDataCellFormat());//这是设置格式

            label.setCellFormat(cf);//这是保持原有格式,但是好像有点不对劲
            wsheet.addCell(label);  
        }  
        book.write();  
    } catch (Exception e) {  
        System.out.println(e);  
    } finally {  
        try {  
            book.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        } catch (WriteException e) {
e.printStackTrace();
}  
    }
}
// 设置标注的格式为黄底红字  (这是设置格式的)
    public static WritableCellFormat getDataCellFormat() {  
        WritableCellFormat wcf = null;  
        try {  
            WritableFont wf = new WritableFont(WritableFont.TIMES, 10,  
                    WritableFont.BOLD, false);  
            // 字体颜色  
            wf.setColour(Colour.RED);  
            wcf = new WritableCellFormat(wf);  
            // 对齐方式  
            wcf.setAlignment(Alignment.CENTRE);  
            wcf.setVerticalAlignment(VerticalAlignment.CENTRE);  
            // 设置上边框  
            wcf.setBorder(Border.TOP, BorderLineStyle.THIN);  
            // 设置下边框  
            wcf.setBorder(Border.BOTTOM, BorderLineStyle.THIN);  
            // 设置左边框  
            wcf.setBorder(Border.LEFT, BorderLineStyle.THIN);  
            // 设置右边框  
            wcf.setBorder(Border.RIGHT, BorderLineStyle.THIN);  
            // 设置背景色  
            wcf.setBackground(Colour.YELLOW);  
            // 自动换行  
            wcf.setWrap(true);  
        } catch (WriteException e) {  
            e.printStackTrace();  
        }  
        return wcf;  
    }
}
0 0
原创粉丝点击