package com.excel.imports;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.UnderlineStyle;
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 org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
* 操作Excel表格的功能类
*/
public class poi1 {
private POIFSFileSystem fs;
private HSSFWorkbook wb;
private HSSFSheet sheet;
private HSSFRow row;
public static void handleSheet2(String excelPath,String outputPath){
WritableWorkbook book = null;
try {
List<Object> a = new ArrayList<Object>();
ArrayList title = new ArrayList();
title.add("车次编号");
title.add("站点名称");
title.add("发车时间");
title.add("离站时间");
a.add(title);
poi1 excelReader = new poi1();
InputStream is = new FileInputStream(excelPath);
ArrayList<Object> map = excelReader.readExcelContentSheet2(is);
System.out.println("获得Excel表格的内容:");
ArrayList<String> list = new ArrayList<String>();
for (int i = 1; i < map.size(); i++) {
String str = map.get(i).toString();
if(!str.startsWith(",,,,")){
list.add(str);
}
}
for(int i = 0; i < list.size(); i++){
String str = list.get(i);
int x = str.indexOf(",,", 3);
if(x>0){
list.set(i, str.substring(0,x));
}
}
for (int i = 0; i < list.size(); i++) {
String str = list.get(i);
if(str.startsWith(",,")){
String s3[] = str.split(",");
String s2[] = list.get(i-1).split(",");
String s1[] = list.get(i-2).split(",");
//System.out.println("s1:"+s1.length+",s2:"+s2.length+",s3:"+s3.length);
for(int j=1;j<s2.length;j++){
ArrayList list1 = new ArrayList();
list1.add(s1[0]);
list1.add(s1[j]);
list1.add(s2[j]);
if(j==s2.length-1){
list1.add("");
}else{
list1.add(s3[j]);
}
a.add(list1);
//System.out.println(s1[0]+" "+s1[j-1]+" "+s2[j-1]+" "+s3[j-1]);
}
}
}
if (a.size() > 0) {
book = Workbook.createWorkbook(new File(outputPath));
WritableFont wf = new WritableFont(WritableFont.ARIAL, 14,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK); // 格式 字体 下划线 斜体 粗体 颜色
WritableCellFormat wcf = new WritableCellFormat(wf);
wcf.setAlignment(Alignment.CENTRE);// 把水平对齐方式指定为
wcf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 把垂直对齐方式指定为居中
WritableSheet sheet1 = book.createSheet("北站到达", 0);
sheet1.mergeCells(0, 0, 4, 0); // 合并单元格
sheet1.setRowView(0, 500); // 设置行高
sheet1.setColumnView(0, 18); // 设置列宽
sheet1.addCell(new Label(0, 0,"青岛到达",
wcf));
for (int z = 0; z < a.size(); z++) {
ArrayList f = (ArrayList) a.get(z);
sheet1.setColumnView(0, 15);
sheet1.setColumnView(1, 15);
sheet1.setColumnView(2, 18);
sheet1.setColumnView(3, 30);
sheet1.addCell(new Label(0, z + 1, f.get(0).toString(), wcf));
sheet1.addCell(new Label(1, z + 1, String.valueOf(f.get(1)), wcf));
sheet1.addCell(new Label(2, z + 1, String.valueOf(f.get(2)), wcf));
sheet1.addCell(new Label(3, z + 1, f.get(3).toString(), wcf));
}
}
for(String str:list){
System.out.println(str);
}
} catch (Exception e) {
// TODO: handle exception
}finally {
if (book != null) {
try {
book.write();
book.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void handleSheet3(String excelPath,String outputPath,int page){
WritableWorkbook book = null;
try {
List<Object> a = new ArrayList<Object>();
ArrayList title = new ArrayList();
title.add("车次编号");
title.add("站点名称");
title.add("发车时间");
title.add("离站时间");
a.add(title);
poi1 excelReader = new poi1();
InputStream is = new FileInputStream(excelPath);
ArrayList<Object> map =null;
if(page==1){
map= excelReader.readExcelContentSheet3(is);
}else if(page==2){
map= excelReader.readExcelContentSheet3_2(is);
}
System.out.println("获得Excel表格的内容:");
ArrayList<String> list = new ArrayList<String>();
for (int i = 1; i < map.size(); i++) {
String str = map.get(i).toString();
if(!str.startsWith(",,,,")){
list.add(str);
System.out.println(str);
}
}
for(int i = 0; i < list.size(); i++){
String str = list.get(i);
int x = str.indexOf(",,", 3);
if(x>0){
list.set(i, str.substring(0,x));
}
}
for (int i = 0; i < list.size(); i++) {
String str = list.get(i);
if(str.startsWith(",,")){
String s3[] = str.split(",");
String s2[] = list.get(i-1).split(",");
String s1[] = list.get(i-2).split(",");
//System.out.println("s1:"+s1.length+",s2:"+s2.length+",s3:"+s3.length);
for(int j=1;j<s2.length;j++){
ArrayList list1 = new ArrayList();
list1.add(s1[0]);
list1.add(s1[j]);
list1.add(s2[j]);
if(j==s2.length-1){
list1.add("");
}else{
list1.add(s3[j]);
}
a.add(list1);
//System.out.println(s1[0]+" "+s1[j-1]+" "+s2[j-1]+" "+s3[j-1]);
}
}
}
if (a.size() > 0) {
book = Workbook.createWorkbook(new File(outputPath));
WritableFont wf = new WritableFont(WritableFont.ARIAL, 14,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK); // 格式 字体 下划线 斜体 粗体 颜色
WritableCellFormat wcf = new WritableCellFormat(wf);
wcf.setAlignment(Alignment.CENTRE);// 把水平对齐方式指定为
wcf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 把垂直对齐方式指定为居中
WritableSheet sheet1 = book.createSheet("北站到达", 0);
sheet1.mergeCells(0, 0, 4, 0); // 合并单元格
sheet1.setRowView(0, 500); // 设置行高
sheet1.setColumnView(0, 18); // 设置列宽
sheet1.addCell(new Label(0, 0,"青岛到达",
wcf));
for (int z = 0; z < a.size(); z++) {
ArrayList f = (ArrayList) a.get(z);
sheet1.setColumnView(0, 15);
sheet1.setColumnView(1, 15);
sheet1.setColumnView(2, 18);
sheet1.setColumnView(3, 30);
sheet1.addCell(new Label(0, z + 1, f.get(0).toString(), wcf));
sheet1.addCell(new Label(1, z + 1, String.valueOf(f.get(1)), wcf));
sheet1.addCell(new Label(2, z + 1, String.valueOf(f.get(2)), wcf));
sheet1.addCell(new Label(3, z + 1, f.get(3).toString(), wcf));
}
}
for(String str:list){
System.out.println(str);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
if (book != null) {
try {
book.write();
book.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
//生成第一个文件
//HandleSheet1("d://青岛火车站(含北站)212.10时刻表.xls","F://青岛始发.xls");
//生成第二个文件
//handleSheet2("d://青岛火车站(含北站)212.10时刻表.xls","F://青岛到达.xls");
handleSheet3("d://青岛火车站(含北站)212.10时刻表.xls","F://北站始发.xls",1);
//handleSheet3("d://青岛火车站(含北站)212.10时刻表.xls","F://北站到达.xls",2);
}
/**
* 读取Excel表格表头的内容
* @param InputStream
* @return String 表头内容的数组
*/
public String[] readExcelTitle(InputStream is) {
try {
fs = new POIFSFileSystem(is);
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
}
sheet = wb.getSheetAt(0);
row = sheet.getRow(0);
// 标题总列数
int colNum = row.getPhysicalNumberOfCells();
System.out.println("colNum:" + colNum);
String[] title = new String[colNum];
for (int i = 0; i < colNum; i++) {
//title[i] = getStringCellValue(row.getCell((short) i));
title[i] = getCellFormatValue(row.getCell((short) i));
}
return title;
}
/**
* 读取Excel数据内容
* @param InputStream
* @return Map 包含单元格数据内容的Map对象
*/
public ArrayList<Object> readExcelContentSheet1(InputStream is) {
ArrayList<Object> content = new ArrayList<Object>();
String str = "";
try {
fs = new POIFSFileSystem(is);
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
}
sheet = wb.getSheetAt(0);
// 得到总行数
int rowNum = sheet.getLastRowNum();
row = sheet.getRow(0);
int colNum = row.getPhysicalNumberOfCells();
// 正文内容应该从第二行开始,第一行为表头的标题
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
int j = 0;
while (j < 31) {
// 每个单元格的数据内容用"-"分割开,以后需要时用String类的replace()方法还原数据
// 也可以将每个单元格的数据设置到一个javabean的属性中,此时需要新建一个javabean
// str += getStringCellValue(row.getCell((short) j)).trim() +
// "-";
str += getCellFormatValue(row.getCell((short) j)).trim() + ",";
j++;
}
if(str.trim().length()==0){
str = "";
continue;
}else{
content.add(str.trim());
str = "";
}
}
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
int j = 31;
while (j < 65) {
// 每个单元格的数据内容用"-"分割开,以后需要时用String类的replace()方法还原数据
// 也可以将每个单元格的数据设置到一个javabean的属性中,此时需要新建一个javabean
// str += getStringCellValue(row.getCell((short) j)).trim() +
// "-";
str += getCellFormatValue(row.getCell((short) j)).trim() + ",";
j++;
}
if(str.trim().length()==0){
str = "";
continue;
}else{
content.add(str.trim());
str = "";
}
}
return content;
}
/**
* 读取Excel数据内容
* @param InputStream
* @return Map 包含单元格数据内容的Map对象
*/
public ArrayList<Object> readExcelContentSheet2(InputStream is) {
ArrayList<Object> content = new ArrayList<Object>();
String str = "";
try {
fs = new POIFSFileSystem(is);
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
}
sheet = wb.getSheetAt(1);
// 得到总行数
int rowNum = sheet.getLastRowNum();
row = sheet.getRow(0);
int colNum = row.getPhysicalNumberOfCells();
// 正文内容应该从第二行开始,第一行为表头的标题
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
int j = 0;
while (j < 34) {
// 每个单元格的数据内容用"-"分割开,以后需要时用String类的replace()方法还原数据
// 也可以将每个单元格的数据设置到一个javabean的属性中,此时需要新建一个javabean
// str += getStringCellValue(row.getCell((short) j)).trim() +
// "-";
str += getCellFormatValue(row.getCell((short) j)).trim() + ",";
j++;
}
if(str.trim().length()==0){
str = "";
continue;
}else{
content.add(str.trim());
str = "";
}
}
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
int j = 34;
while (j < 65) {
// 每个单元格的数据内容用"-"分割开,以后需要时用String类的replace()方法还原数据
// 也可以将每个单元格的数据设置到一个javabean的属性中,此时需要新建一个javabean
// str += getStringCellValue(row.getCell((short) j)).trim() +
// "-";
str += getCellFormatValue(row.getCell((short) j)).trim() + ",";
j++;
}
if(str.trim().length()==0){
str = "";
continue;
}else{
content.add(str.trim());
str = "";
}
}
return content;
}
/**
* 读取Excel数据内容
* @param InputStream
* @return Map 包含单元格数据内容的Map对象
*/
public ArrayList<Object> readExcelContentSheet3_2(InputStream is) {
ArrayList<Object> content = new ArrayList<Object>();
String str = "";
try {
fs = new POIFSFileSystem(is);
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
}
sheet = wb.getSheetAt(2);
// 得到总行数
int rowNum = sheet.getLastRowNum();
row = sheet.getRow(0);
int colNum = row.getPhysicalNumberOfCells();
// 正文内容应该从第二行开始,第一行为表头的标题
for (int i = 75; i <= colNum; i++) {
row = sheet.getRow(i);
int j = 0;
while (j < colNum) {
str += getCellFormatValue(row.getCell((short) j)).trim() + ",";
j++;
}
if(str.trim().length()==0){
str = "";
continue;
}else{
content.add(str.trim());
str = "";
}
}
return content;
}
/**
* 读取Excel数据内容
* @param InputStream
* @return Map 包含单元格数据内容的Map对象
*/
public ArrayList<Object> readExcelContentSheet3(InputStream is) {
ArrayList<Object> content = new ArrayList<Object>();
String str = "";
try {
fs = new POIFSFileSystem(is);
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
}
sheet = wb.getSheetAt(2);
// 得到总行数
int rowNum = sheet.getLastRowNum();
row = sheet.getRow(0);
int colNum = row.getPhysicalNumberOfCells();
// 正文内容应该从第二行开始,第一行为表头的标题
for (int i = 0; i <= 70; i++) {
row = sheet.getRow(i);
int j = 0;
while (j < colNum) {
// 每个单元格的数据内容用"-"分割开,以后需要时用String类的replace()方法还原数据
// 也可以将每个单元格的数据设置到一个javabean的属性中,此时需要新建一个javabean
// str += getStringCellValue(row.getCell((short) j)).trim() +
// "-";
/* if(row.getCell(j)!=null){
if(row.getCell(j).getCellType()==(row.getCell((short) j).CELL_TYPE_NUMERIC)){
//row.getCell((short) j).setCellType(row.getCell((short) j).CELL_TYPE_STRING);
System.out.println(row.getCell(j));
}
}*/
str += getCellFormatValue(row.getCell((short) j)).trim() + ",";
j++;
}
if(str.trim().length()==0){
str = "";
continue;
}else{
content.add(str.trim());
str = "";
}
}
return content;
}
/**
* 获取单元格数据内容为字符串类型的数据
*
* @param cell Excel单元格
* @return String 单元格数据内容
*/
private String getStringCellValue(HSSFCell cell) {
String strCell = "";
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
strCell = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
strCell = String.valueOf(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
strCell = String.valueOf(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK:
strCell = "";
break;
default:
strCell = "";
break;
}
if (strCell.equals("") || strCell == null) {
return "";
}
if (cell == null) {
return "";
}
return strCell;
}
/**
* 获取单元格数据内容为日期类型的数据
*
* @param cell
* Excel单元格
* @return String 单元格数据内容
*/
private String getDateCellValue(HSSFCell cell) {
String result = "";
try {
int cellType = cell.getCellType();
if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
Date date = cell.getDateCellValue();
result = (date.getYear() + 1900) + "-" + (date.getMonth() + 1)
+ "-" + date.getDate();
} else if (cellType == HSSFCell.CELL_TYPE_STRING) {
String date = getStringCellValue(cell);
result = date.replaceAll("[年月]", "-").replace("日", "").trim();
} else if (cellType == HSSFCell.CELL_TYPE_BLANK) {
result = "";
}
} catch (Exception e) {
System.out.println("日期格式不正确!");
e.printStackTrace();
}
return result;
}
/**
* 根据HSSFCell类型设置数据
* @param cell
* @return
*/
private String getCellFormatValue(HSSFCell cell) {
String cellvalue = "";
if (cell != null) {
// 判断当前Cell的Type
switch (cell.getCellType()) {
// 如果当前Cell的Type为NUMERIC
case HSSFCell.CELL_TYPE_NUMERIC:
case HSSFCell.CELL_TYPE_FORMULA: {
// 判断当前的cell是否为Date
if (HSSFDateUtil.isCellDateFormatted(cell)) {
// 如果是Date类型则,转化为Data格式
//方法1:这样子的data格式是带时分秒的:2011-10-12 0:00:00
//cellvalue = cell.getDateCellValue().toLocaleString();
//方法2:这样子的data格式是不带带时分秒的:2011-10-12
Date date = cell.getDateCellValue();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
cellvalue = sdf.format(date);
}
// 如果是纯数字
else {
// 取得当前Cell的数值
cellvalue = String.valueOf("");
}
break;
}
// 如果当前Cell的Type为STRIN
case HSSFCell.CELL_TYPE_STRING:
// 取得当前的Cell字符串
cellvalue = cell.getRichStringCellValue().getString();
break;
// 默认的Cell值
default:
cellvalue = " ";
}
} else {
cellvalue = "";
}
return cellvalue;
}
public static void HandleSheet1(String importExcelPath,String outExcelPath){
WritableWorkbook book = null;
try {
// 对读取Excel表格标题测试
InputStream is = new FileInputStream(importExcelPath);
poi1 excelReader = new poi1();
String title1 = excelReader.readExcelTitle(is)[0];
System.out.println("获得Excel表格的标题:");
System.out.println(title1);
// 对读取Excel表格内容测试
List<Object> a = new ArrayList<Object>();
ArrayList title = new ArrayList();
title.add("车次编号");
title.add("站点名称");
title.add("发车时间");
title.add("离站时间");
a.add(title);
InputStream is2 = new FileInputStream(importExcelPath);
ArrayList<Object> map = excelReader.readExcelContentSheet1(is2);
System.out.println("获得Excel表格的内容:");
ArrayList<String> list = new ArrayList<String>();
for (int i = 1; i < map.size(); i++) {
String str = map.get(i).toString();
if(!str.startsWith(",,,,")){
list.add(str);
}
}
for(int i = 0; i < list.size(); i++){
String str = list.get(i);
int x = str.indexOf(",,", 3);
if(x>0){
list.set(i, str.substring(0,x));
}
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).toString());
}
for (int i = 0; i < list.size(); i++) {
String str = list.get(i);
if(str.startsWith(",,")){
String s3[] = str.split(",");
String s2[] = list.get(i-1).split(",");
String s1[] = list.get(i-2).split(",");
//System.out.println("s1:"+s1.length+",s2:"+s2.length+",s3:"+s3.length);
for(int j=1;j<s2.length;j++){
ArrayList list1 = new ArrayList();
list1.add(s1[0]);
list1.add(s1[j]);
list1.add(s2[j]);
if(j==s2.length-1){
list1.add("");
}else{
list1.add(s3[j]);
}
a.add(list1);
//System.out.println(s1[0]+" "+s1[j-1]+" "+s2[j-1]+" "+s3[j-1]);
}
}
}
if (a.size() > 0) {
book = Workbook.createWorkbook(new File(outExcelPath));
WritableFont wf = new WritableFont(WritableFont.ARIAL, 14,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK); // 格式 字体 下划线 斜体 粗体 颜色
WritableCellFormat wcf = new WritableCellFormat(wf);
wcf.setAlignment(Alignment.CENTRE);// 把水平对齐方式指定为
wcf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 把垂直对齐方式指定为居中
WritableSheet sheet1 = book.createSheet("北站到达", 0);
sheet1.mergeCells(0, 0, 4, 0); // 合并单元格
sheet1.setRowView(0, 500); // 设置行高
sheet1.setColumnView(0, 18); // 设置列宽
sheet1.addCell(new Label(0, 0,title1,
wcf));
for (int z = 0; z < a.size(); z++) {
ArrayList f = (ArrayList) a.get(z);
sheet1.setColumnView(0, 15);
sheet1.setColumnView(1, 15);
sheet1.setColumnView(2, 18);
sheet1.setColumnView(3, 30);
sheet1.addCell(new Label(0, z + 1, f.get(0).toString(), wcf));
sheet1.addCell(new Label(1, z + 1, String.valueOf(f.get(1)), wcf));
sheet1.addCell(new Label(2, z + 1, String.valueOf(f.get(2)), wcf));
sheet1.addCell(new Label(3, z + 1, f.get(3).toString(), wcf));
}
}
} catch (FileNotFoundException e) {
System.out.println("未找到指定路径的文件!");
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if (book != null) {
try {
book.write();
book.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.UnderlineStyle;
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 org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
* 操作Excel表格的功能类
*/
public class poi1 {
private POIFSFileSystem fs;
private HSSFWorkbook wb;
private HSSFSheet sheet;
private HSSFRow row;
public static void handleSheet2(String excelPath,String outputPath){
WritableWorkbook book = null;
try {
List<Object> a = new ArrayList<Object>();
ArrayList title = new ArrayList();
title.add("车次编号");
title.add("站点名称");
title.add("发车时间");
title.add("离站时间");
a.add(title);
poi1 excelReader = new poi1();
InputStream is = new FileInputStream(excelPath);
ArrayList<Object> map = excelReader.readExcelContentSheet2(is);
System.out.println("获得Excel表格的内容:");
ArrayList<String> list = new ArrayList<String>();
for (int i = 1; i < map.size(); i++) {
String str = map.get(i).toString();
if(!str.startsWith(",,,,")){
list.add(str);
}
}
for(int i = 0; i < list.size(); i++){
String str = list.get(i);
int x = str.indexOf(",,", 3);
if(x>0){
list.set(i, str.substring(0,x));
}
}
for (int i = 0; i < list.size(); i++) {
String str = list.get(i);
if(str.startsWith(",,")){
String s3[] = str.split(",");
String s2[] = list.get(i-1).split(",");
String s1[] = list.get(i-2).split(",");
//System.out.println("s1:"+s1.length+",s2:"+s2.length+",s3:"+s3.length);
for(int j=1;j<s2.length;j++){
ArrayList list1 = new ArrayList();
list1.add(s1[0]);
list1.add(s1[j]);
list1.add(s2[j]);
if(j==s2.length-1){
list1.add("");
}else{
list1.add(s3[j]);
}
a.add(list1);
//System.out.println(s1[0]+" "+s1[j-1]+" "+s2[j-1]+" "+s3[j-1]);
}
}
}
if (a.size() > 0) {
book = Workbook.createWorkbook(new File(outputPath));
WritableFont wf = new WritableFont(WritableFont.ARIAL, 14,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK); // 格式 字体 下划线 斜体 粗体 颜色
WritableCellFormat wcf = new WritableCellFormat(wf);
wcf.setAlignment(Alignment.CENTRE);// 把水平对齐方式指定为
wcf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 把垂直对齐方式指定为居中
WritableSheet sheet1 = book.createSheet("北站到达", 0);
sheet1.mergeCells(0, 0, 4, 0); // 合并单元格
sheet1.setRowView(0, 500); // 设置行高
sheet1.setColumnView(0, 18); // 设置列宽
sheet1.addCell(new Label(0, 0,"青岛到达",
wcf));
for (int z = 0; z < a.size(); z++) {
ArrayList f = (ArrayList) a.get(z);
sheet1.setColumnView(0, 15);
sheet1.setColumnView(1, 15);
sheet1.setColumnView(2, 18);
sheet1.setColumnView(3, 30);
sheet1.addCell(new Label(0, z + 1, f.get(0).toString(), wcf));
sheet1.addCell(new Label(1, z + 1, String.valueOf(f.get(1)), wcf));
sheet1.addCell(new Label(2, z + 1, String.valueOf(f.get(2)), wcf));
sheet1.addCell(new Label(3, z + 1, f.get(3).toString(), wcf));
}
}
for(String str:list){
System.out.println(str);
}
} catch (Exception e) {
// TODO: handle exception
}finally {
if (book != null) {
try {
book.write();
book.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void handleSheet3(String excelPath,String outputPath,int page){
WritableWorkbook book = null;
try {
List<Object> a = new ArrayList<Object>();
ArrayList title = new ArrayList();
title.add("车次编号");
title.add("站点名称");
title.add("发车时间");
title.add("离站时间");
a.add(title);
poi1 excelReader = new poi1();
InputStream is = new FileInputStream(excelPath);
ArrayList<Object> map =null;
if(page==1){
map= excelReader.readExcelContentSheet3(is);
}else if(page==2){
map= excelReader.readExcelContentSheet3_2(is);
}
System.out.println("获得Excel表格的内容:");
ArrayList<String> list = new ArrayList<String>();
for (int i = 1; i < map.size(); i++) {
String str = map.get(i).toString();
if(!str.startsWith(",,,,")){
list.add(str);
System.out.println(str);
}
}
for(int i = 0; i < list.size(); i++){
String str = list.get(i);
int x = str.indexOf(",,", 3);
if(x>0){
list.set(i, str.substring(0,x));
}
}
for (int i = 0; i < list.size(); i++) {
String str = list.get(i);
if(str.startsWith(",,")){
String s3[] = str.split(",");
String s2[] = list.get(i-1).split(",");
String s1[] = list.get(i-2).split(",");
//System.out.println("s1:"+s1.length+",s2:"+s2.length+",s3:"+s3.length);
for(int j=1;j<s2.length;j++){
ArrayList list1 = new ArrayList();
list1.add(s1[0]);
list1.add(s1[j]);
list1.add(s2[j]);
if(j==s2.length-1){
list1.add("");
}else{
list1.add(s3[j]);
}
a.add(list1);
//System.out.println(s1[0]+" "+s1[j-1]+" "+s2[j-1]+" "+s3[j-1]);
}
}
}
if (a.size() > 0) {
book = Workbook.createWorkbook(new File(outputPath));
WritableFont wf = new WritableFont(WritableFont.ARIAL, 14,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK); // 格式 字体 下划线 斜体 粗体 颜色
WritableCellFormat wcf = new WritableCellFormat(wf);
wcf.setAlignment(Alignment.CENTRE);// 把水平对齐方式指定为
wcf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 把垂直对齐方式指定为居中
WritableSheet sheet1 = book.createSheet("北站到达", 0);
sheet1.mergeCells(0, 0, 4, 0); // 合并单元格
sheet1.setRowView(0, 500); // 设置行高
sheet1.setColumnView(0, 18); // 设置列宽
sheet1.addCell(new Label(0, 0,"青岛到达",
wcf));
for (int z = 0; z < a.size(); z++) {
ArrayList f = (ArrayList) a.get(z);
sheet1.setColumnView(0, 15);
sheet1.setColumnView(1, 15);
sheet1.setColumnView(2, 18);
sheet1.setColumnView(3, 30);
sheet1.addCell(new Label(0, z + 1, f.get(0).toString(), wcf));
sheet1.addCell(new Label(1, z + 1, String.valueOf(f.get(1)), wcf));
sheet1.addCell(new Label(2, z + 1, String.valueOf(f.get(2)), wcf));
sheet1.addCell(new Label(3, z + 1, f.get(3).toString(), wcf));
}
}
for(String str:list){
System.out.println(str);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}finally {
if (book != null) {
try {
book.write();
book.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
public static void main(String[] args) {
//生成第一个文件
//HandleSheet1("d://青岛火车站(含北站)212.10时刻表.xls","F://青岛始发.xls");
//生成第二个文件
//handleSheet2("d://青岛火车站(含北站)212.10时刻表.xls","F://青岛到达.xls");
handleSheet3("d://青岛火车站(含北站)212.10时刻表.xls","F://北站始发.xls",1);
//handleSheet3("d://青岛火车站(含北站)212.10时刻表.xls","F://北站到达.xls",2);
}
/**
* 读取Excel表格表头的内容
* @param InputStream
* @return String 表头内容的数组
*/
public String[] readExcelTitle(InputStream is) {
try {
fs = new POIFSFileSystem(is);
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
}
sheet = wb.getSheetAt(0);
row = sheet.getRow(0);
// 标题总列数
int colNum = row.getPhysicalNumberOfCells();
System.out.println("colNum:" + colNum);
String[] title = new String[colNum];
for (int i = 0; i < colNum; i++) {
//title[i] = getStringCellValue(row.getCell((short) i));
title[i] = getCellFormatValue(row.getCell((short) i));
}
return title;
}
/**
* 读取Excel数据内容
* @param InputStream
* @return Map 包含单元格数据内容的Map对象
*/
public ArrayList<Object> readExcelContentSheet1(InputStream is) {
ArrayList<Object> content = new ArrayList<Object>();
String str = "";
try {
fs = new POIFSFileSystem(is);
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
}
sheet = wb.getSheetAt(0);
// 得到总行数
int rowNum = sheet.getLastRowNum();
row = sheet.getRow(0);
int colNum = row.getPhysicalNumberOfCells();
// 正文内容应该从第二行开始,第一行为表头的标题
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
int j = 0;
while (j < 31) {
// 每个单元格的数据内容用"-"分割开,以后需要时用String类的replace()方法还原数据
// 也可以将每个单元格的数据设置到一个javabean的属性中,此时需要新建一个javabean
// str += getStringCellValue(row.getCell((short) j)).trim() +
// "-";
str += getCellFormatValue(row.getCell((short) j)).trim() + ",";
j++;
}
if(str.trim().length()==0){
str = "";
continue;
}else{
content.add(str.trim());
str = "";
}
}
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
int j = 31;
while (j < 65) {
// 每个单元格的数据内容用"-"分割开,以后需要时用String类的replace()方法还原数据
// 也可以将每个单元格的数据设置到一个javabean的属性中,此时需要新建一个javabean
// str += getStringCellValue(row.getCell((short) j)).trim() +
// "-";
str += getCellFormatValue(row.getCell((short) j)).trim() + ",";
j++;
}
if(str.trim().length()==0){
str = "";
continue;
}else{
content.add(str.trim());
str = "";
}
}
return content;
}
/**
* 读取Excel数据内容
* @param InputStream
* @return Map 包含单元格数据内容的Map对象
*/
public ArrayList<Object> readExcelContentSheet2(InputStream is) {
ArrayList<Object> content = new ArrayList<Object>();
String str = "";
try {
fs = new POIFSFileSystem(is);
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
}
sheet = wb.getSheetAt(1);
// 得到总行数
int rowNum = sheet.getLastRowNum();
row = sheet.getRow(0);
int colNum = row.getPhysicalNumberOfCells();
// 正文内容应该从第二行开始,第一行为表头的标题
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
int j = 0;
while (j < 34) {
// 每个单元格的数据内容用"-"分割开,以后需要时用String类的replace()方法还原数据
// 也可以将每个单元格的数据设置到一个javabean的属性中,此时需要新建一个javabean
// str += getStringCellValue(row.getCell((short) j)).trim() +
// "-";
str += getCellFormatValue(row.getCell((short) j)).trim() + ",";
j++;
}
if(str.trim().length()==0){
str = "";
continue;
}else{
content.add(str.trim());
str = "";
}
}
for (int i = 1; i <= rowNum; i++) {
row = sheet.getRow(i);
int j = 34;
while (j < 65) {
// 每个单元格的数据内容用"-"分割开,以后需要时用String类的replace()方法还原数据
// 也可以将每个单元格的数据设置到一个javabean的属性中,此时需要新建一个javabean
// str += getStringCellValue(row.getCell((short) j)).trim() +
// "-";
str += getCellFormatValue(row.getCell((short) j)).trim() + ",";
j++;
}
if(str.trim().length()==0){
str = "";
continue;
}else{
content.add(str.trim());
str = "";
}
}
return content;
}
/**
* 读取Excel数据内容
* @param InputStream
* @return Map 包含单元格数据内容的Map对象
*/
public ArrayList<Object> readExcelContentSheet3_2(InputStream is) {
ArrayList<Object> content = new ArrayList<Object>();
String str = "";
try {
fs = new POIFSFileSystem(is);
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
}
sheet = wb.getSheetAt(2);
// 得到总行数
int rowNum = sheet.getLastRowNum();
row = sheet.getRow(0);
int colNum = row.getPhysicalNumberOfCells();
// 正文内容应该从第二行开始,第一行为表头的标题
for (int i = 75; i <= colNum; i++) {
row = sheet.getRow(i);
int j = 0;
while (j < colNum) {
str += getCellFormatValue(row.getCell((short) j)).trim() + ",";
j++;
}
if(str.trim().length()==0){
str = "";
continue;
}else{
content.add(str.trim());
str = "";
}
}
return content;
}
/**
* 读取Excel数据内容
* @param InputStream
* @return Map 包含单元格数据内容的Map对象
*/
public ArrayList<Object> readExcelContentSheet3(InputStream is) {
ArrayList<Object> content = new ArrayList<Object>();
String str = "";
try {
fs = new POIFSFileSystem(is);
wb = new HSSFWorkbook(fs);
} catch (IOException e) {
e.printStackTrace();
}
sheet = wb.getSheetAt(2);
// 得到总行数
int rowNum = sheet.getLastRowNum();
row = sheet.getRow(0);
int colNum = row.getPhysicalNumberOfCells();
// 正文内容应该从第二行开始,第一行为表头的标题
for (int i = 0; i <= 70; i++) {
row = sheet.getRow(i);
int j = 0;
while (j < colNum) {
// 每个单元格的数据内容用"-"分割开,以后需要时用String类的replace()方法还原数据
// 也可以将每个单元格的数据设置到一个javabean的属性中,此时需要新建一个javabean
// str += getStringCellValue(row.getCell((short) j)).trim() +
// "-";
/* if(row.getCell(j)!=null){
if(row.getCell(j).getCellType()==(row.getCell((short) j).CELL_TYPE_NUMERIC)){
//row.getCell((short) j).setCellType(row.getCell((short) j).CELL_TYPE_STRING);
System.out.println(row.getCell(j));
}
}*/
str += getCellFormatValue(row.getCell((short) j)).trim() + ",";
j++;
}
if(str.trim().length()==0){
str = "";
continue;
}else{
content.add(str.trim());
str = "";
}
}
return content;
}
/**
* 获取单元格数据内容为字符串类型的数据
*
* @param cell Excel单元格
* @return String 单元格数据内容
*/
private String getStringCellValue(HSSFCell cell) {
String strCell = "";
switch (cell.getCellType()) {
case HSSFCell.CELL_TYPE_STRING:
strCell = cell.getStringCellValue();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
strCell = String.valueOf(cell.getNumericCellValue());
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
strCell = String.valueOf(cell.getBooleanCellValue());
break;
case HSSFCell.CELL_TYPE_BLANK:
strCell = "";
break;
default:
strCell = "";
break;
}
if (strCell.equals("") || strCell == null) {
return "";
}
if (cell == null) {
return "";
}
return strCell;
}
/**
* 获取单元格数据内容为日期类型的数据
*
* @param cell
* Excel单元格
* @return String 单元格数据内容
*/
private String getDateCellValue(HSSFCell cell) {
String result = "";
try {
int cellType = cell.getCellType();
if (cellType == HSSFCell.CELL_TYPE_NUMERIC) {
Date date = cell.getDateCellValue();
result = (date.getYear() + 1900) + "-" + (date.getMonth() + 1)
+ "-" + date.getDate();
} else if (cellType == HSSFCell.CELL_TYPE_STRING) {
String date = getStringCellValue(cell);
result = date.replaceAll("[年月]", "-").replace("日", "").trim();
} else if (cellType == HSSFCell.CELL_TYPE_BLANK) {
result = "";
}
} catch (Exception e) {
System.out.println("日期格式不正确!");
e.printStackTrace();
}
return result;
}
/**
* 根据HSSFCell类型设置数据
* @param cell
* @return
*/
private String getCellFormatValue(HSSFCell cell) {
String cellvalue = "";
if (cell != null) {
// 判断当前Cell的Type
switch (cell.getCellType()) {
// 如果当前Cell的Type为NUMERIC
case HSSFCell.CELL_TYPE_NUMERIC:
case HSSFCell.CELL_TYPE_FORMULA: {
// 判断当前的cell是否为Date
if (HSSFDateUtil.isCellDateFormatted(cell)) {
// 如果是Date类型则,转化为Data格式
//方法1:这样子的data格式是带时分秒的:2011-10-12 0:00:00
//cellvalue = cell.getDateCellValue().toLocaleString();
//方法2:这样子的data格式是不带带时分秒的:2011-10-12
Date date = cell.getDateCellValue();
SimpleDateFormat sdf = new SimpleDateFormat("HH:mm");
cellvalue = sdf.format(date);
}
// 如果是纯数字
else {
// 取得当前Cell的数值
cellvalue = String.valueOf("");
}
break;
}
// 如果当前Cell的Type为STRIN
case HSSFCell.CELL_TYPE_STRING:
// 取得当前的Cell字符串
cellvalue = cell.getRichStringCellValue().getString();
break;
// 默认的Cell值
default:
cellvalue = " ";
}
} else {
cellvalue = "";
}
return cellvalue;
}
public static void HandleSheet1(String importExcelPath,String outExcelPath){
WritableWorkbook book = null;
try {
// 对读取Excel表格标题测试
InputStream is = new FileInputStream(importExcelPath);
poi1 excelReader = new poi1();
String title1 = excelReader.readExcelTitle(is)[0];
System.out.println("获得Excel表格的标题:");
System.out.println(title1);
// 对读取Excel表格内容测试
List<Object> a = new ArrayList<Object>();
ArrayList title = new ArrayList();
title.add("车次编号");
title.add("站点名称");
title.add("发车时间");
title.add("离站时间");
a.add(title);
InputStream is2 = new FileInputStream(importExcelPath);
ArrayList<Object> map = excelReader.readExcelContentSheet1(is2);
System.out.println("获得Excel表格的内容:");
ArrayList<String> list = new ArrayList<String>();
for (int i = 1; i < map.size(); i++) {
String str = map.get(i).toString();
if(!str.startsWith(",,,,")){
list.add(str);
}
}
for(int i = 0; i < list.size(); i++){
String str = list.get(i);
int x = str.indexOf(",,", 3);
if(x>0){
list.set(i, str.substring(0,x));
}
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).toString());
}
for (int i = 0; i < list.size(); i++) {
String str = list.get(i);
if(str.startsWith(",,")){
String s3[] = str.split(",");
String s2[] = list.get(i-1).split(",");
String s1[] = list.get(i-2).split(",");
//System.out.println("s1:"+s1.length+",s2:"+s2.length+",s3:"+s3.length);
for(int j=1;j<s2.length;j++){
ArrayList list1 = new ArrayList();
list1.add(s1[0]);
list1.add(s1[j]);
list1.add(s2[j]);
if(j==s2.length-1){
list1.add("");
}else{
list1.add(s3[j]);
}
a.add(list1);
//System.out.println(s1[0]+" "+s1[j-1]+" "+s2[j-1]+" "+s3[j-1]);
}
}
}
if (a.size() > 0) {
book = Workbook.createWorkbook(new File(outExcelPath));
WritableFont wf = new WritableFont(WritableFont.ARIAL, 14,
WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
jxl.format.Colour.BLACK); // 格式 字体 下划线 斜体 粗体 颜色
WritableCellFormat wcf = new WritableCellFormat(wf);
wcf.setAlignment(Alignment.CENTRE);// 把水平对齐方式指定为
wcf.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);// 把垂直对齐方式指定为居中
WritableSheet sheet1 = book.createSheet("北站到达", 0);
sheet1.mergeCells(0, 0, 4, 0); // 合并单元格
sheet1.setRowView(0, 500); // 设置行高
sheet1.setColumnView(0, 18); // 设置列宽
sheet1.addCell(new Label(0, 0,title1,
wcf));
for (int z = 0; z < a.size(); z++) {
ArrayList f = (ArrayList) a.get(z);
sheet1.setColumnView(0, 15);
sheet1.setColumnView(1, 15);
sheet1.setColumnView(2, 18);
sheet1.setColumnView(3, 30);
sheet1.addCell(new Label(0, z + 1, f.get(0).toString(), wcf));
sheet1.addCell(new Label(1, z + 1, String.valueOf(f.get(1)), wcf));
sheet1.addCell(new Label(2, z + 1, String.valueOf(f.get(2)), wcf));
sheet1.addCell(new Label(3, z + 1, f.get(3).toString(), wcf));
}
}
} catch (FileNotFoundException e) {
System.out.println("未找到指定路径的文件!");
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
if (book != null) {
try {
book.write();
book.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}