package cn.jiguang.base64;java
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;app
import jxl.Cell;
import jxl.CellType;
import jxl.NumberCell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;.net
/**
*
* @Description: Excel自動導出txt,構建java代碼
* @Author: zengxiaochi
*
* @Date: 下午11:37:27
*/
public class ExcelExport {excel
public static void main(String[] args) {
// TODO Auto-generated method stub
String filepath = "sjd_pdl.xls";ip
excelOutTxt(filepath);
}rem
private static void excelOutTxt(String filepath) {
try {
Workbook workbook = Workbook.getWorkbook(new File(filepath));
Sheet sheet = workbook.getSheet("配置表");
Sheet sheet2 = workbook.getSheet("模型邏輯");
// rows爲行數,columns列數,getCell("列號","行號")
int rows = sheet2.getRows();
int columns = sheet2.getColumns();
Cell c2 = null;
NumberCell numberCellOne = null;
List<String> strList = new ArrayList<String>();
List<Object> doubleList = new ArrayList<Object>();字符串
// 用來記錄要比較的字符串get
List<String> storage = new ArrayList<String>();
for (int i2 = 0; i2 < rows; i2++) {
for (int j2 = 0; j2 < columns; j2++) {
// 根據橫縱軸獲取單元格數據
c2 = sheet2.getCell(j2, i2);
if (c2.getColumn() == 0) {
strList.add(c2.getContents());
} else if (c2.getColumn() == 1) {
if (CellType.NUMBER == c2.getType()) {
numberCellOne = (NumberCell) c2;
doubleList.add(numberCellOne.getValue());
} else {
doubleList.add(c2.getContents());
}
}string
}
}
for (Object object : doubleList) {
System.out.println(object);
}
System.out.println("==============");
for (String str : strList) {
System.out.println(str);
}it
File fileSuccess = new File("success.txt");
FileWriter fwSuccess = new FileWriter(fileSuccess);
BufferedWriter bwSuccess = new BufferedWriter(fwSuccess);
// j爲行數,getCell("列號","行號")
int j = sheet.getRows();
int y = sheet.getColumns();
Cell c = null;
NumberCell numberCell = null;
for (int i = 0; i < j; i++) {
StringBuffer stringBuffer = new StringBuffer();
for (int x = 0; x < y; x++) {
c = sheet.getCell(x, i);
if (CellType.NUMBER == c.getType()) {
numberCell = (NumberCell) c;
}
if (c.getColumn() == 0) {
storage.add(c.getContents());
stringBuffer.append("antifraudSjdModelEntityList.add(new AntifraudSjdModelEntity( " + '"'
+ c.getContents() + '"');
} else if (c.getColumn() == 1 || c.getColumn() == 2) {
continue;
} else if ("inf".equals(c.getContents())) {
stringBuffer.append(", " + "Double.MAX_VALUE");
} else if ("-inf".equals(c.getContents())) {
stringBuffer.append(", " + "-Double.MAX_VALUE");
} else if (c.getColumn() == 5) {
if (CellType.NUMBER == c.getType()) {
stringBuffer.append(", " + numberCell.getValue());
} else {
stringBuffer.append(", " + c.getContents());
}
} else if (c.getColumn() == 6) {
for (int k = 0; k < strList.size(); k++) {
if (storage.get(0).trim().equals(strList.get(k).trim())) {
stringBuffer.append(", " + doubleList.get(k) + "));");
}
}
storage.remove(0);
} else if ("".equals(c.getContents())) {
stringBuffer.append(", " + "null");
} else {
if (CellType.NUMBER == c.getType()) {
stringBuffer.append(", " + numberCell.getValue());
} else {
stringBuffer.append(", " + c.getContents());
}
}
}
bwSuccess.write(stringBuffer.toString()); bwSuccess.write(" "); bwSuccess.flush(); // 讀取一行後換行 bwSuccess.newLine(); // 刷新 } System.out.println("寫入結束"); } catch (BiffException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }