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();//获取第一个单元格的格式
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;
}
}
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
- Excel更新单元格的内容
- 【C#】C#删除Excel整行,更新单元格内容
- Excel合并多个单元格的内容。
- POI获取excel单元格的内容
- DataGridView的单元格内容即时更新方法
- excel 合并单元格内容
- excel 隐藏单元格内容
- excel 更改单元格内容
- Excel彻底清除单元格内容
- excel 单元格增加内容
- VB更改EXCEL单元格内容
- java获取Excel单元格内容
- EXCEL单元格内容自动换行
- excel 合并单元格内容
- EXCEL获取单元格内容方法
- 批量替换Excel表格中非空的单元格的内容
- Excel 2007单元格及内容的合并、拆分_C#教程
- Excel 2007单元格及内容的合并、拆分_C#教程
- Hive限制
- poj3735,矩阵快速幂
- framebuffer 简介
- Hardcoded string "下一步", should use @string resource警告
- 导出和导入AWR的信息(ORACLE)
- Excel更新单元格的内容
- Codility-Flags
- VS与Matlab混合编译 - mexw64
- 2、CRM2011编程实战——设置窗体某一项的值为服务器时间
- CI框架UEditor编辑器PHP版无法获取POST值
- STL容器学习总结
- 内存分区那些事
- 编写gstreamill的job文件
- 浅谈 Python 的 with 语句