如何有效读取Excel中的日期数据?
import jxl.*;
import jxl.format.UnderlineStyle;
import jxl.write.*;
import jxl.write.number;
import jxl.write.Boolean;
import java.io.*;
class excel {
public excel() {
}
/**
* 读取excel
*
* @param filepath
*/
public static void readexcel(String filepath) {
try {
InputStream is = new FileInputStream(filepath);
workbook rwb = Workbook.getWorkbook(is);
// sheet st = rwb.getsheet("0")这里有两种方法获取sheet表,1为名字,2为下标,从0开始
Sheet st = rwb.getSheet(0);
Cell c00 = st.getCell(1, 0);
// 通用的获取cell值的方式,返回字符串
String strc00 = c00.getContents();
// 获得cell具体类型值的方式
if (c00.getType() == CellType.LABEL) {
LabelCell labelc00 = (LabelCell) c00;
strc00 = labelc00.getString();
}
// 输出
System.out.println(strc00);
// 关闭
rwb.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 输出excel
*
* @param os
*/
public static void writeexcel(OutputStream os) {
try {
/**
* 只能通过api提供的工厂方法来创建workbook,而不能使用WritableWorkbook的构造函数,
* 因为类WritableWorkbook的构造函数为protected类型
* method(1)直接从目标文件中读取WritableWorkbook wwb =
* workbook.createWorkbook(new file(targetfile)); method(2)如下实例所示
* 将WritableWorkbook直接写入到输出流
*
*/
WritableWorkbook wwb = Workbook.createWorkbook(os);
// 创建excel工作表 指定名称和位置
WritableSheet ws = wwb.createSheet("test sheet 1", 0);
WritableSheet ws2 = wwb.createSheet("test sheet 2", 1);
// **************往工作表中添加数据*****************
// 1.添加label对象
Label label = new Label(0, 0, "this is a label test");
ws.addCell(label);
Label label2 = new Label(0, 0, "this is a label test2");
ws2.addCell(label2);
// 添加带有字型formatting对象
WritableFont wf = new WritableFont(WritableFont.TIMES, 18,
WritableFont.BOLD, true);
WritableCellFormat wcf = new WritableCellFormat(wf);
Label labelcf = new Label(1, 0, "this is a label test", wcf);
ws.addCell(labelcf);
// 添加带有字体颜色的formatting对象
WritableFont wfc = new WritableFont(WritableFont.ARIAL, 10,
WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.RED);
WritableCellFormat wcffc = new WritableCellFormat(wfc);
Label Labelcf = new Label(1, 0, "this is a Label cell", wcffc);
ws.addCell(Labelcf);
// 2.添加number对象
Number Labeln = new Number(0, 1, 3.1415926);
ws.addCell(Labeln);
// 添加带有formatting的number对象
NumberFormat nf = new NumberFormat("#.##");
WritableCellFormat wcfn = new WritableCellFormat(nf);
Number Labelnf = new jxl.write.Number(1, 1, 3.1415926, wcfn);
ws.addCell(Labelnf);
// 3.添加boolean对象
Boolean Labelb = new jxl.write.Boolean(0, 2, false);
ws.addCell(Labelb);
// 4.添加datetime对象
jxl.write.DateTime Labeldt = new jxl.write.DateTime(0, 3,
new java.util.Date());
ws.addCell(Labeldt);
// 添加带有formatting的dateformat对象
DateFormat df = new DateFormat("dd mm yyyy hh:mm:ss");
WritableCellFormat wcfdf = new WritableCellFormat(df);
DateTime Labeldtf = new DateTime(1, 3, new java.util.Date(), wcfdf);
ws.addCell(Labeldtf);
// 添加图片对象,jxl只支持png格式图片
// File image = new File("f:\\2.png");
// WritableImage wimage = new WritableImage(0,1,2,2,image);
// ws.addImage(wimage);
// 写入工作表
wwb.write();
wwb.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 拷贝后,进行修改,其中File1为被copy对象,File2为修改后创建的对象
* 尽单元格原有的格式化修饰是不能去掉的,我们还是可以将新的单元格修饰加上去, 以使单元格的内容以不同的形式表现
*
* @param File1
* @param File2
*/
public static void modifyexcel(File File1, File File2) {
try {
Workbook rwb = Workbook.getWorkbook(File1);
WritableWorkbook wwb = Workbook.createWorkbook(File2, rwb);// copy
WritableSheet ws = wwb.getSheet(0);
WritableCell wc = ws.getWritableCell(0, 0);
// 判断单元格的类型,做出相应的转换
Copyright © 广州京杭网络科技有限公司 2005-2024 版权所有 粤ICP备16019765号
广州京杭网络科技有限公司 版权所有