📄 createexcel.java
字号:
package net.aetherial.gis.excel;
import java.io.*;
import jxl.*;
import jxl.format.*;
import jxl.write.*;
public class CreateExcel {
public static int begin =0;
private WritableWorkbook workbook = null;
private WritableSheet sheet = null;
public CreateExcel() {
}
public void createWorkbook(String filePath) { //filePath 指创建文件的地址 default:"D:\\GIS\\Excel\\test.xls"
try {
workbook = Workbook.createWorkbook(new File(filePath));
}
catch (Exception e) {
e.printStackTrace();
}
}
public void createSheet(String sheetName, int position) {
try {
this.sheet = workbook.createSheet(sheetName, position);
}
catch (Exception e) {
e.printStackTrace();
}
}
public WritableCell createWritableCell(Cell cell) { //把cell转化成WritableCell,包括行和列
WritableCell wc = null;
int col = cell.getColumn(), row = cell.getRow();
WritableCellFormat mywcf = null;
String myStr ="______________市_____________县(区)______________乡________________村 道路类型:拟建/已建 编制日期: 年 月 日";
try {
//wc = WritableCell.copyTo(col,row);
///*
if (cell.getType() == CellType.LABEL) {
WritableFont wf = new WritableFont(WritableFont.TIMES,14,WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE);
WritableCellFormat wcf = new WritableCellFormat();
wcf.setAlignment(jxl.format.Alignment.CENTRE);
wcf.setWrap(true);
wcf.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
this.sheet.mergeCells(0,begin+1,17,begin+1);
Label label = new Label(col, row, myStr, wcf);
wc = label;
}
else if (cell.getType() == CellType.NUMBER) {
//mywcf =cell.getCellFormat();
jxl.write.Number number = new jxl.write.Number(col, row,
Double.parseDouble(cell.getContents()), cell.getCellFormat());
wc = number;
}
else {
Label label = new Label(col, row, "", cell.getCellFormat());
wc = label;
}
// */
}
catch (Exception e) {
e.printStackTrace();
}
return wc;
}
public void writeIt(int col, int row, Sheet sheet) {
Cell cell = sheet.getCell(col, row);
WritableCell wc = this.createWritableCell(cell);
ExcelStandard es = new ExcelStandard();
WritableCell wc1 = es.row1(0,this.sheet);
es.initialize(this.sheet);
try {
this.sheet.addCell(wc);
this.sheet.addCell(wc1);
}
catch (Exception e) {
e.printStackTrace();
}
}
public void closeWorkbook() {
try {
this.workbook.write();
this.workbook.close();
}
catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String args[]) {
ReadStandard rs = new ReadStandard(
"D:\\GIS\\Excel\\standard\\standard1.xls");
Sheet sheet = rs.getSheetStandard(0);
//int col=3,row =10;
CreateExcel ce = new CreateExcel();
ce.createWorkbook("D:\\GIS\\Excel\\test.xls");
ce.createSheet("test", 0);
//for (int i = 0; i < 18; i++) {
// for (int j = 0; j < 32; j++) {
ce.writeIt(0, 1, sheet);
// }
// }
ce.createSheet("test1", 1);
ce.writeIt(0, 1, sheet);
ce.closeWorkbook();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -