package cn.veryedu.excel;
import java.io.File;
import java.io.IOException;
import jxl.Workbook;
import jxl.format.CellFormat;
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.BoldStyle;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.Alignment;
import jxl.format.VerticalAlignment;
import jxl.write.biff.RowsExceededException;
public class WriteExcel {
public static void main(String[] args) {
WritableWorkbook workbook = null;
try {
workbook = Workbook.createWorkbook(new File("C:\\demo.xls"));
} catch (IOException e) {
e.printStackTrace();
}
//创建工作表
WritableSheet sheet = workbook.createSheet("工作表", 0);
//定义单元格的格式
WritableFont font = new WritableFont(WritableFont.TAHOMA, 18, WritableFont.BOLD);//用于大标题
WritableFont font2 = new WritableFont(WritableFont.ARIAL, 15, WritableFont.BOLD);//用于排名...
WritableFont font3 = new WritableFont(WritableFont.TIMES, 12, WritableFont.BOLD);//用于内容
WritableCellFormat cellFormat = new WritableCellFormat(font);
WritableCellFormat cellFormat2 = new WritableCellFormat(font2);
WritableCellFormat cellFormat3 = new WritableCellFormat(font3);
try {
//设置单元格边框
cellFormat.setBorder(Border.ALL, BorderLineStyle.THIN);
cellFormat2.setBorder(Border.ALL, BorderLineStyle.THIN);
cellFormat3.setBorder(Border.ALL, BorderLineStyle.THIN);
//单元格背景颜色
cellFormat.setBackground(Colour.YELLOW);
// 单元格内内容居中
cellFormat.setAlignment(Alignment.CENTRE);
cellFormat2.setAlignment(Alignment.CENTRE);
cellFormat3.setAlignment(Alignment.CENTRE);
//垂直对齐
cellFormat.setVerticalAlignment(VerticalAlignment.CENTRE);
cellFormat2.setVerticalAlignment(VerticalAlignment.CENTRE);
cellFormat3.setVerticalAlignment(VerticalAlignment.CENTRE);
} catch (WriteException e1) {
e1.printStackTrace();
}
//合并单元格
try {
sheet.mergeCells(5,2, 14, 5);
sheet.mergeCells(5, 6, 6, 7);
sheet.mergeCells(7, 6, 8, 7);
sheet.mergeCells(9, 6, 10, 7);
sheet.mergeCells(11, 6, 12, 7);
sheet.mergeCells(13, 6, 14, 7);
//创建单元格并写入内容
WritableCell cell = new Label(5,2,"指标明细报表",cellFormat);
WritableCell rankingCell = new Label(5,6,"排名",cellFormat2);
WritableCell eleNameCell = new Label(7,6,"机构名称",cellFormat2);
WritableCell eleType = new Label(9,6,"机构类型",cellFormat2);
WritableCell wcz = new Label(11,6,"实际完成",cellFormat2);
WritableCell score = new Label(13,6,"得分",cellFormat2);
//向工作表 追加单元格内容
sheet.addCell(cell);
sheet.addCell(rankingCell);
sheet.addCell(eleNameCell);
sheet.addCell(eleType);
sheet.addCell(wcz);
sheet.addCell(score);
} catch (RowsExceededException e1) {
e1.printStackTrace();
} catch (WriteException e1) {
e1.printStackTrace();
}
try {
//根据设定的内容生成Excel文档
workbook.write();
workbook.close();
} catch (IOException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
}
}
----------------------------------------
package cn.veryedu.excel;
import java.io.File;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class ReadExcel {
public static void main(String[] args) {
try {
Workbook workbook = Workbook.getWorkbook(new File("学生信息.xls"));
Sheet sheet = workbook.getSheet(0);//获取Excel文件的工作表
Cell[] cells = sheet.getColumn(4); //获取学号列
int endRow = cells[cells.length - 1].getRow();
for (int i = 11; i <= endRow; i++) {
System.out.print(" " + sheet.getCell(4, i).getContents());
System.out.print(" " + sheet.getCell(6, i).getContents());
System.out.print(" " + sheet.getCell(8, i).getContents());
System.out.println();
}
workbook.close();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}