我有一个简单的程序,它使用 apache.poi 外部库来操作 excel 文件。我在 windows 环境中使用了 eclipse,现在我必须使用终端在 linux 上编译和运行我的程序。我搜索了如何包含我需要的那些罐子,但最终出现以下错误:
错误:无法找到或加载主类 xlsToCsv。
我遵循的步骤是这样的:
javac -cp ./jars/poi-3.13-20150929.jar:./jars/poi-ooxml-schemas-3.13-20150929.jar:./jars/poi-ooxml-3.13-20150929.jar:./jars/xmlbeans -2.6.0.jar xlsToCsv.java
java -cp ./jars/poi-3.13-20150929.jar:./jars/poi-ooxml-schemas-3.13-20150929.jar:./jars/poi-ooxml-3.13-20150929.jar:./jars/xmlbeans -2.6.0.jar xlsToCsv
我当前的目录是 /home/demo/Desktop/xls_to_csv xlsToCsv.java 文件在哪里。jar 文件位于 /home/demo/Desktop/xls_to_csv/jars 中。
任何人都可以告诉我并解释正确的语法吗?是否可以调用包含所有 jar 文件的文件夹而不是单独调用它们?
提前致谢。
编辑,我的代码:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.DataFormatter;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
public class xlsToCsv {
static void convert(File input, File output) throws Exception {
StringBuffer data = new StringBuffer();
FileOutputStream fos = new FileOutputStream(output);
HSSFWorkbook file = new HSSFWorkbook(new FileInputStream(input));
DataFormatter objDefaultFormat = new DataFormatter();
FormulaEvaluator objFormulaEvaluator = new HSSFFormulaEvaluator((HSSFWorkbook) file);
Sheet sheet;
Row row;
Cell cell;
String sheetName, cellValue;
int cellType, rowIndex = 0, columnIndex = 0;
boolean blankRow = true, blankArea = true;
Iterator<Sheet> sheetIt;
Iterator<Row> rowIt;
Iterator<Cell> cellIt;
sheetIt = file.iterator();
while(sheetIt.hasNext()) {
sheet = sheetIt.next();
sheetName = sheet.getSheetName();
rowIt = sheet.iterator();
while(rowIt.hasNext()) {
row = rowIt.next();
cellIt = row.iterator();
while (cellIt.hasNext()) {
cell = cellIt.next();
cellType = cell.getCellType();
if(!isBlankCell(cellType) || !blankArea) {
if(rowIndex == 0 && columnIndex == 0)
data.append(sheetName + " - Header" + ";");
else if(rowIndex > 0 && columnIndex == 0)
data.append(sheetName + ";");
objFormulaEvaluator.evaluateInCell(cell);
cellValue = objDefaultFormat.formatCellValue(cell,objFormulaEvaluator);
if(cellValue.isEmpty())
data.append(";");
else
data.append(cellValue + ";");
columnIndex++;
blankRow = false;
blankArea = false;
}
}
if(!blankRow) {
data.append('\n');
rowIndex++;
}
blankRow = true;
columnIndex = 0;
}
//new sheet => reset control fields
rowIndex = 0;
columnIndex = 0;
blankRow = true;
blankArea = true;
}
fos.write(data.toString().getBytes());
fos.close();
}
private static boolean isBlankCell(int cellType) {
return cellType == Cell.CELL_TYPE_BLANK
|| cellType == Cell.CELL_TYPE_ERROR
|| cellType == Cell.CELL_TYPE_FORMULA;
}
public static void main(String[] args) {
if(args.length < 2 || args.length > 2) {
System.err.println("Insert input and output path");
System.exit(0);
}
File input = new File(args[0]);
File output = new File(args[1]);
try {
convert(input, output);
System.out.println("File " + output.getName() + "created sucessfully");
} catch(Exception e) {
e.printStackTrace();
}
}
}