Jexcel完成按必定規矩朋分excel文件的辦法。本站提示廣大學習愛好者:(Jexcel完成按必定規矩朋分excel文件的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是Jexcel完成按必定規矩朋分excel文件的辦法正文
本文實例講述了Jexcel完成按必定規矩朋分excel文件的辦法。分享給年夜家供年夜家參考。詳細以下:
現有一個excel文檔,須要讀取它並依照必定的規矩,朋分之,朋分出來的每段記載須要零丁創立一個excel文檔並寫入個中,必定要包管單位格格局的分歧性。
package edu.bjut.zhutong.excelParser;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import jxl.Cell;
import jxl.CellType;
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.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class ExcelParser {
public static void main(String[] args) {
Workbook wb = null;
try {
//取得了Workbook對象以後,便可以經由過程它獲得Sheet(任務表)對象了
InputStream is = new FileInputStream("C:/excel/excel.xls");
wb = Workbook.getWorkbook(is);
// 取得第一個任務表對象
Sheet sheet = wb.getSheet(0);
//取得任務表的行數和列數
int rows = sheet.getRows();
int cols = sheet.getColumns();
System.out.println("一共 " + rows + " 行");
System.out.println("一共 " + cols + " 列");
int counter = 0; //任務表行游標
int fileCounts = 1; //用來標識創立的excel文檔數量
while(counter<rows-1) {
//獲得counter行的一切單位格
Cell[] rowCells = sheet.getRow(counter);
Cell cell0 = rowCells[0];
//斷定單位格內容的類型
if(cell0.getType() == CellType.LABEL) {
System.out.println("正在解析第 " + fileCounts + " 個文件....");
//新建一個excel文檔
File file = new File("C:/excel/excel" + fileCounts + ".xls");
WritableWorkbook wwb = Workbook.createWorkbook(file);
//設置excel文檔的任務表
WritableSheet ws = wwb.createSheet("sheet1", 0);
//第一行歸並第0到第8列
ws.mergeCells(0, 0, 8, 0);
//設置第7,8,9列的列寬
ws.setColumnView(6, 10);
ws.setColumnView(7, 45);
ws.setColumnView(8, 27);
//向新建的表中寫入數據,起首第一行先寫入題目
for(int k=0; k<rowCells.length; k++) {
//創立WritableFont對象用來格局化字體,這裡是20號宋體,加粗
WritableFont wf = new WritableFont(WritableFont.createFont("宋體"), 20, WritableFont.BOLD, false);
//應用WritableFont創立單位格格局化對象
WritableCellFormat wcf = new WritableCellFormat(wf);
//設置程度對齊方法
wcf.setAlignment(Alignment.CENTRE);
//設置垂直對齊方法
wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
//設置邊框和色彩
wcf.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
Cell cell = rowCells[k];
Label label = new Label(k,0,cell.getContents(),wcf);
//添加單位格到表中
ws.addCell(label);
//設置第一行的行高
ws.setRowView(0, 30*20, false);
}
//向新建的表中寫入數據,第二行寫入表頭
for(int c=0; c<cols; c++) {
String colCon = sheet.getCell(c, 1).getContents();
WritableFont wf = new WritableFont(WritableFont.createFont("宋體"), 12, WritableFont.BOLD, false);
WritableCellFormat wcf = new WritableCellFormat(wf);
wcf.setAlignment(Alignment.CENTRE);
wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
wcf.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
ws.addCell(new Label(c,1,colCon,wcf));
ws.setRowView(1, 18*20, false);
}
int rowCounts = 1; //用來遍歷50
counter++; //將游標挪動到下一行
if(counter == 1) //假如游標到了第二行 ,就主動把游標挪動到第三行,第二行不須要處置
counter = 2;
int rowIndex = 2; //每篇excel文檔的游標
rowCells = sheet.getRow(counter);
cell0 = rowCells[0];
while(cell0.getType() == CellType.NUMBER && counter<rows-1) {
rowCells = sheet.getRow(counter);
for(int k=0; k<rowCells.length; k++) {
WritableFont wf = new WritableFont(WritableFont.createFont("宋體"), 12, WritableFont.NO_BOLD, false);
WritableCellFormat wcf = new WritableCellFormat(wf);
wcf.setAlignment(Alignment.CENTRE);
wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
wcf.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
Label label = new Label(k,rowIndex,rowCells[k].getContents(),wcf);
ws.addCell(label);
}
//用來處置備注列的邊框
{
WritableFont wf = new WritableFont(WritableFont.createFont("宋體"), 12, WritableFont.NO_BOLD, false);
WritableCellFormat wcf = new WritableCellFormat(wf);
wcf.setAlignment(Alignment.CENTRE);
wcf.setVerticalAlignment(VerticalAlignment.CENTRE);
wcf.setBorder(Border.ALL, BorderLineStyle.THIN, Colour.BLACK);
Label label = new Label(8,rowIndex,"",wcf);
ws.addCell(label);
}
ws.setRowView(rowIndex, 18*20, false);
rowIndex++;
counter++;
cell0 = sheet.getRow(counter)[0];
}
wwb.write();
wwb.close();
fileCounts++;
}
}
System.out.println("法式履行停止....");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
} finally {
wb.close(); //封閉Workbook對象
}
}
}
願望本文所述對年夜家的java法式設計有所贊助。