package exceltest;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.LabelCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.read.biff.BiffException;
import jxl.write.Boolean;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.NumberFormat;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableImage;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class Excel {
public static List list = new ArrayList();
public static void main(String[] args) {
// 读取Excel
// realExcel("D:/testRead.xls");
// 输出Excel
File fileWrite = new File("D:/testWrite.xls");
try {
fileWrite.createNewFile();
OutputStream output = new FileOutputStream(fileWrite);
writeExcel(output);
} catch (IOException e) {
e.printStackTrace();
}
//修改Excel
updateExcel(new File("D:/testRead.xls"),new File("D:/testWrite.xls"));
}
/*
* 修改Excel
* @file :
*/
private static void updateExcel(File file, File file2) {
try {
Workbook workbook = Workbook.getWorkbook(file);
WritableWorkbook writBook = Workbook.createWorkbook(file2,workbook);
WritableSheet sheet = writBook.getSheet(0);
WritableCell cell = sheet.getWritableCell(0,0);
//判断单元格的类型,做出相应的转换
if(cell.getType() == CellType.LABEL){
Label label = (Label)cell;
label.setString("my name is xiaohui");
}
writBook.write();
writBook.close();
workbook.close();
} catch (BiffException | IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
/*
* 写入Exel方法。
*/
@SuppressWarnings("static-access")
public static void writeExcel(OutputStream output) {
WritableWorkbook workbook = null;
try {
workbook = Workbook.createWorkbook(output);
WritableSheet sheet = workbook.createSheet("Test Sheet 1", 0);
// 1.添加Label对象:
Label label = new Label(0, 0, "this is a label test");
sheet.addCell(label);
// 添加字型为Formatting的对象:
WritableFont writFont = new WritableFont(WritableFont.TIMES, 18, WritableFont.BOLD, true);
WritableCellFormat writCellFormat = new WritableCellFormat(writFont);
Label labelCell = new Label(1, 0, "my name is Keylin", writCellFormat);
sheet.addCell(labelCell);
// 添加带有字体颜色的Formattiong对象。
WritableFont writFontCell = new WritableFont(writFont.ARIAL, 10, writFont.NO_BOLD, false,
UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLUE);
WritableCellFormat writCellFormatColor = new WritableCellFormat(writFontCell);
Label labelColor = new Label(1,0,"my name is Keylin",writCellFormatColor);
sheet.addCell(labelColor);
//2.添加Number 对象。
Number labeNu = new Number(0,1,3.1415926);
sheet.addCell(labeNu);
//添加带有formattiong的Number对象
NumberFormat numberFormat = new NumberFormat("#.##");
WritableCellFormat writCellFormatNu = new WritableCellFormat(numberFormat);
Number labeNumber = new Number(1,1,3.1415926,writCellFormatNu);
sheet.addCell(labeNumber);
//添加boolean对象
Boolean LabelBoo = new Boolean(0,2,false);
sheet.addCell(LabelBoo);
//添加图片对象,jxl只支持png格式图片
File image = new File("F:/tools/apkTool_new/apkTool/HWSupportMobile_android/source/bin/res/drawable-ldpi/icon.png");
WritableImage writImage = new WritableImage(0,1,2,2,image);
sheet.addImage(writImage);
workbook.write();
workbook.close();
} catch (IOException e) {
e.printStackTrace();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
/*
* 读取Excel的方法
*
*/
public static void realExcel(String filePath) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Workbook workbook = null;
try {
InputStream file = new FileInputStream(filePath);
// 构造Workbook(工作薄)对象
workbook = Workbook.getWorkbook(file);
// 创建一个sheet(工作页)的对象
Sheet sheet = workbook.getSheet("Sheet1");
// 得到工作页的列跟行数。
int lows = sheet.getColumns();
int rows = sheet.getRows();
for (int i = 0; i < lows; i++) {
for (int j = 0; j < rows; j++) {
// 获取该行列的Cell(单元格)
Cell cell = sheet.getCell(i, j);
// 获取Cell对象,返回字符串。
String strc = cell.getContents();
// 获取具体类型值得方式;
if (cell.getType() == CellType.LABEL) {
LabelCell labelc = (LabelCell) cell;
strc = labelc.getString();
}
// 类型为时间的处理。
if (cell.getType() == CellType.DATE) {
DateCell dateCell = (DateCell) cell;
strc = format.format(dateCell.getDate());
}
System.out.println("第" + i + "列:" + "\t" + strc);
list.add(strc);
}
System.out.println();
}
workbook.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import jxl.Cell;
import jxl.CellType;
import jxl.DateCell;
import jxl.LabelCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.format.UnderlineStyle;
import jxl.read.biff.BiffException;
import jxl.write.Boolean;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.NumberFormat;
import jxl.write.WritableCell;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableImage;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class Excel {
public static List list = new ArrayList();
public static void main(String[] args) {
// 读取Excel
// realExcel("D:/testRead.xls");
// 输出Excel
File fileWrite = new File("D:/testWrite.xls");
try {
fileWrite.createNewFile();
OutputStream output = new FileOutputStream(fileWrite);
writeExcel(output);
} catch (IOException e) {
e.printStackTrace();
}
//修改Excel
updateExcel(new File("D:/testRead.xls"),new File("D:/testWrite.xls"));
}
/*
* 修改Excel
* @file :
*/
private static void updateExcel(File file, File file2) {
try {
Workbook workbook = Workbook.getWorkbook(file);
WritableWorkbook writBook = Workbook.createWorkbook(file2,workbook);
WritableSheet sheet = writBook.getSheet(0);
WritableCell cell = sheet.getWritableCell(0,0);
//判断单元格的类型,做出相应的转换
if(cell.getType() == CellType.LABEL){
Label label = (Label)cell;
label.setString("my name is xiaohui");
}
writBook.write();
writBook.close();
workbook.close();
} catch (BiffException | IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
/*
* 写入Exel方法。
*/
@SuppressWarnings("static-access")
public static void writeExcel(OutputStream output) {
WritableWorkbook workbook = null;
try {
workbook = Workbook.createWorkbook(output);
WritableSheet sheet = workbook.createSheet("Test Sheet 1", 0);
// 1.添加Label对象:
Label label = new Label(0, 0, "this is a label test");
sheet.addCell(label);
// 添加字型为Formatting的对象:
WritableFont writFont = new WritableFont(WritableFont.TIMES, 18, WritableFont.BOLD, true);
WritableCellFormat writCellFormat = new WritableCellFormat(writFont);
Label labelCell = new Label(1, 0, "my name is Keylin", writCellFormat);
sheet.addCell(labelCell);
// 添加带有字体颜色的Formattiong对象。
WritableFont writFontCell = new WritableFont(writFont.ARIAL, 10, writFont.NO_BOLD, false,
UnderlineStyle.NO_UNDERLINE, jxl.format.Colour.BLUE);
WritableCellFormat writCellFormatColor = new WritableCellFormat(writFontCell);
Label labelColor = new Label(1,0,"my name is Keylin",writCellFormatColor);
sheet.addCell(labelColor);
//2.添加Number 对象。
Number labeNu = new Number(0,1,3.1415926);
sheet.addCell(labeNu);
//添加带有formattiong的Number对象
NumberFormat numberFormat = new NumberFormat("#.##");
WritableCellFormat writCellFormatNu = new WritableCellFormat(numberFormat);
Number labeNumber = new Number(1,1,3.1415926,writCellFormatNu);
sheet.addCell(labeNumber);
//添加boolean对象
Boolean LabelBoo = new Boolean(0,2,false);
sheet.addCell(LabelBoo);
//添加图片对象,jxl只支持png格式图片
File image = new File("F:/tools/apkTool_new/apkTool/HWSupportMobile_android/source/bin/res/drawable-ldpi/icon.png");
WritableImage writImage = new WritableImage(0,1,2,2,image);
sheet.addImage(writImage);
workbook.write();
workbook.close();
} catch (IOException e) {
e.printStackTrace();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
/*
* 读取Excel的方法
*
*/
public static void realExcel(String filePath) {
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
Workbook workbook = null;
try {
InputStream file = new FileInputStream(filePath);
// 构造Workbook(工作薄)对象
workbook = Workbook.getWorkbook(file);
// 创建一个sheet(工作页)的对象
Sheet sheet = workbook.getSheet("Sheet1");
// 得到工作页的列跟行数。
int lows = sheet.getColumns();
int rows = sheet.getRows();
for (int i = 0; i < lows; i++) {
for (int j = 0; j < rows; j++) {
// 获取该行列的Cell(单元格)
Cell cell = sheet.getCell(i, j);
// 获取Cell对象,返回字符串。
String strc = cell.getContents();
// 获取具体类型值得方式;
if (cell.getType() == CellType.LABEL) {
LabelCell labelc = (LabelCell) cell;
strc = labelc.getString();
}
// 类型为时间的处理。
if (cell.getType() == CellType.DATE) {
DateCell dateCell = (DateCell) cell;
strc = format.format(dateCell.getDate());
}
System.out.println("第" + i + "列:" + "\t" + strc);
list.add(strc);
}
System.out.println();
}
workbook.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}