Java POI Excel API到底需要什么
我正在开发通过POI API 3.9生成Excel数据的程序 但目录中有许多Jar文件,如下所示Java POI Excel API到底需要什么,java,apache-poi,jexcelapi,Java,Apache Poi,Jexcelapi,我正在开发通过POI API 3.9生成Excel数据的程序 但目录中有许多Jar文件,如下所示 poi-3.9-20121203 poi-excelant-3.9-20121203 poi-ooxml-3.9-20121203 poi-ooxml-schemas-3.9-20121203 poi-scratchpad-3.9-20121203 ooxml-lib/xmlbeans-2.3.0 lib/commons-logging-1.1 ..etc. 我只需要创建,阅读和
poi-3.9-20121203
poi-excelant-3.9-20121203
poi-ooxml-3.9-20121203
poi-ooxml-schemas-3.9-20121203
poi-scratchpad-3.9-20121203
ooxml-lib/xmlbeans-2.3.0
lib/commons-logging-1.1
..etc.
我只需要创建,阅读和编写excel文件。我不知道需要什么jar文件
请告诉我要导入哪些文件。我在网站上读了很多文件。但是我可以找到关于这个的
谢谢在您的情况下,不要太担心依赖关系。只需使用或之类的工具,然后添加: Maven:
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
这将为您提供正确的依赖项。您可能需要导入所有依赖项。与许多框架一样,ApachePOI被划分到不同的库中。ApachePOI还使用XML bean等外部库 在上,您将找到组件列表以及需要实现的目标,请查看该部分 通常,如果只处理旧的Excel文件,则不需要
poi ooxml
依赖项
请注意,您的外部依赖项列表列在组件映射部分第二个表的先决条件列中,如其中所述,您还需要其他库:commons logging
,commons codec
,log4j
最后,为了避免依赖关系管理带来的麻烦,您可以使用Maven之类的工具来为您解决这个问题。对于独立项目,您需要以下JAR commons-logging-1.1.jar dom4j-1.6.1.jar jsr173_1.0_api.jar log4j-1.2.13.jar poi-3.8-20120326.jar poi-ooxml-3.8-20120326.jar poi-ooxml-schemas-3.8-20120326.jar 解析器.jar xbean_xpath.jar xbean.jar xmlbeans-qname.jar
jar您只需要5个jar就可以完成上述任务 例如,如果我们使用的是Apache3.9
dom4j-1.6.1.jar
poi-3.9-20121203.jar
poi-ooxml-3.9-0121203.jar
poi-ooxml-schemas-3.9-0121203.jar
xmlbeans-2.3.0.jar
如果使用Maven进行项目构建,则只需在项目上添加以下两个依赖项即可-
<!-- Apache POI -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.9</version>
</dependency>
org.apache.poi
是我开发的演示项目,演示excel阅读方法。以上几行来自我的项目pom。这种方法对我来说很好。(1)嗨,我在这里发布了一个链接,您可以从中获得与您的问题相关的所有信息
(2) 您可以从此URL下载所有必需的JAR
(3) 提取URL并在类路径中包含所有JAR
(4) 对于MS Excel示例,您可以查看以下网站。它有所有需要的例子
您可以使用3.9版或3.8版。但我发现poi版本3.9删除了till在poi-3.8-20120326.jar上提供的一些类(例如org.apache.poi.xssf.usermodel.XSSFWorkbook)。因此,请验证这些类是否用于您的项目
希望这有帮助 感谢下面的链接
为我工作:)
工作编辑代码:
public static boolean isExternalStorageReadOnly() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
return true;
}
return false;
}
public static boolean isExternalStorageAvailable() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
return true;
}
return false;
}
private static boolean saveExcelFile(MainActivity mainActivity, String fileName) {
// check if available and not read only
if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
Log.w("FileUtils", "Storage not available or read only");
return false;
}
boolean success = false;
//New Workbook
Workbook wb = new HSSFWorkbook();
Cell c = null;
//Cell style for header row
CellStyle cs = wb.createCellStyle();
cs.setFillForegroundColor(HSSFColor.LIME.index);
cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//New Sheet
Sheet sheet1 = null;
sheet1 = wb.createSheet("Sheet1");
// Generate column headings
Row row = sheet1.createRow(0);
c = row.createCell(0);
c.setCellValue(DBHelper.INVENTORY_PROJECT_CODE);
c.setCellStyle(cs);
c = row.createCell(1);
c.setCellValue(DBHelper.INVENTORY_PROJECT_UPC_BAR_CODE);
c.setCellStyle(cs);
c = row.createCell(2);
c.setCellValue(DBHelper.INVENTORY_PROJECT_NAME);
c.setCellStyle(cs);
c = row.createCell(3);
c.setCellValue(DBHelper.INVENTORY_PROJECT_QUANTITY);
c.setCellStyle(cs);
c = row.createCell(4);
c.setCellValue(DBHelper.INVENTORY_PROJECT_PRICE);
c.setCellStyle(cs);
ArrayList<String> arrayList1 = dbHelper.getAllRecords();
int rowno = 1;
int columnno=0;
for(String red : arrayList1){
columnno = 0;
Row row1 = sheet1.createRow(rowno);
String[] parts = red.split(Pattern.quote(","));
for(String str : parts){
c = row1.createCell(columnno);
c.setCellValue(str);
c.setCellStyle(cs);
columnno++;
}
rowno++;
}
sheet1.setColumnWidth(0, (15 * 500));
sheet1.setColumnWidth(1, (15 * 500));
sheet1.setColumnWidth(2, (15 * 500));
sheet1.setColumnWidth(3, (15 * 500));
sheet1.setColumnWidth(4, (15 * 500));
// Create a path where we will place our List of objects on external storage
File file = new File(mainActivity.getExternalFilesDir(null), fileName);
FileOutputStream os = null;
try {
os = new FileOutputStream(file);
wb.write(os);
Log.w("FileUtils", "Writing file" + file);
success = true;
} catch (IOException e) {
Log.w("FileUtils", "Error writing " + file, e);
} catch (Exception e) {
Log.w("FileUtils", "Failed to save file", e);
} finally {
try {
if (null != os)
os.close();
} catch (Exception ex) {
}
}
return success;
}
public静态布尔值isExternalStorageReadOnly(){
字符串extStorageState=Environment.getExternalStorageState();
if(Environment.MEDIA\u MOUNTED\u READ\u ONLY.equals(extStorageState)){
返回true;
}
返回false;
}
公共静态布尔值isExternalStorageAvailable(){
字符串extStorageState=Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(extStorageState)){
返回true;
}
返回false;
}
私有静态布尔saveExcelFile(MainActivity MainActivity,字符串文件名){
//检查是否可用,而不是只读
如果(!isExternalStorageAvailable()| | isExternalStorageReadOnly()){
Log.w(“FileUtils”、“存储不可用或只读”);
返回false;
}
布尔成功=假;
//新工作簿
工作簿wb=新的HSSF工作簿();
细胞c=null;
//标题行的单元格样式
CellStyle cs=wb.createCellStyle();
cs.setFillForegroundColor(HSSFColor.LIME.index);
cs.setFillPattern(HSSFCellStyle.SOLID_前景);
//新表
第1页=空;
sheet1=wb.createSheet(“sheet1”);
//生成列标题
Row Row=sheet1.createRow(0);
c=行。创建单元(0);
c、 setCellValue(DBHelper.INVENTORY\u项目\u代码);
c、 赛特风格(cs);
c=行。创建单元(1);
c、 setCellValue(DBHelper.INVENTORY\u PROJECT\u UPC\u条形码);
c、 赛特风格(cs);
c=行。创建单元(2);
c、 setCellValue(DBHelper.INVENTORY\u PROJECT\u NAME);
c、 赛特风格(cs);
c=行。创建单元(3);
c、 setCellValue(DBHelper.INVENTORY\u PROJECT\u QUANTITY);
c、 赛特风格(cs);
c=行。创建单元(4);
c、 setCellValue(DBHelper.INVENTORY\u PROJECT\u PRICE);
c、 赛特风格(cs);
ArrayList arrayList1=dbHelper.getAllRecords();
int-rowno=1;
int columnno=0;
用于(红色字符串:arrayList1){
columnno=0;
行row1=sheet1.createRow(行号);
String[]parts=red.split(Pattern.quote(“,”);
用于(字符串str:parts){
c=行1.createCell(列号);
c、 setCellValue(str);
c、 赛特风格(cs);
columnno++;
}
rowno++;
}
图1.设置柱宽(0,(15*500));
图1.设置柱宽(1,(15*500));
图1.设置柱宽(2,(15*500));
图1.设置柱宽(3,(15*500));
图1.设置柱宽(4,(15*500));
//创建一个路径,在该路径中,我们将在外部存储器上放置对象列表
File File=新文件(mainActivity.getExternalFilesDir(null),文件名);
FileOutputStream os=null;
试一试{
os=新文件输出流(文件);
写操作系统;
Log.w(“FileUtils”,“Writing file”+file);
成功=真实;
}捕获(IOE异常){
Log.w(“FileUtils”,“写入错误”+文件,e);
public static boolean isExternalStorageReadOnly() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
return true;
}
return false;
}
public static boolean isExternalStorageAvailable() {
String extStorageState = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
return true;
}
return false;
}
private static boolean saveExcelFile(MainActivity mainActivity, String fileName) {
// check if available and not read only
if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
Log.w("FileUtils", "Storage not available or read only");
return false;
}
boolean success = false;
//New Workbook
Workbook wb = new HSSFWorkbook();
Cell c = null;
//Cell style for header row
CellStyle cs = wb.createCellStyle();
cs.setFillForegroundColor(HSSFColor.LIME.index);
cs.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
//New Sheet
Sheet sheet1 = null;
sheet1 = wb.createSheet("Sheet1");
// Generate column headings
Row row = sheet1.createRow(0);
c = row.createCell(0);
c.setCellValue(DBHelper.INVENTORY_PROJECT_CODE);
c.setCellStyle(cs);
c = row.createCell(1);
c.setCellValue(DBHelper.INVENTORY_PROJECT_UPC_BAR_CODE);
c.setCellStyle(cs);
c = row.createCell(2);
c.setCellValue(DBHelper.INVENTORY_PROJECT_NAME);
c.setCellStyle(cs);
c = row.createCell(3);
c.setCellValue(DBHelper.INVENTORY_PROJECT_QUANTITY);
c.setCellStyle(cs);
c = row.createCell(4);
c.setCellValue(DBHelper.INVENTORY_PROJECT_PRICE);
c.setCellStyle(cs);
ArrayList<String> arrayList1 = dbHelper.getAllRecords();
int rowno = 1;
int columnno=0;
for(String red : arrayList1){
columnno = 0;
Row row1 = sheet1.createRow(rowno);
String[] parts = red.split(Pattern.quote(","));
for(String str : parts){
c = row1.createCell(columnno);
c.setCellValue(str);
c.setCellStyle(cs);
columnno++;
}
rowno++;
}
sheet1.setColumnWidth(0, (15 * 500));
sheet1.setColumnWidth(1, (15 * 500));
sheet1.setColumnWidth(2, (15 * 500));
sheet1.setColumnWidth(3, (15 * 500));
sheet1.setColumnWidth(4, (15 * 500));
// Create a path where we will place our List of objects on external storage
File file = new File(mainActivity.getExternalFilesDir(null), fileName);
FileOutputStream os = null;
try {
os = new FileOutputStream(file);
wb.write(os);
Log.w("FileUtils", "Writing file" + file);
success = true;
} catch (IOException e) {
Log.w("FileUtils", "Error writing " + file, e);
} catch (Exception e) {
Log.w("FileUtils", "Failed to save file", e);
} finally {
try {
if (null != os)
os.close();
} catch (Exception ex) {
}
}
return success;
}