我想将excel转换为xml [英] I want to convert excel to xml
问题描述
我想将excel转换为xml.谁能告诉我如何使用mapper?我的代码:
公共类WorkGroupReader实现ItemReader>{WorkGroupMapper 线映射器;public void setLinemapper(WorkGroupMapper linemapper) {this.linemapper = linemapper;}@覆盖公共列表<字符串>read() 抛出异常、UnexpectedInputException、ParseException、NonTransientResourceException、BiffException、IOException {String FilePath = "E:\\ide-workspaces\\OmniFeed\\input\\WorkGroup.xls";FileInputStream fs = new FileInputStream(FilePath);工作簿 wb = Workbook.getWorkbook(fs);//获取对工作表的访问权限Sheet sh = wb.getSheet("WorkGroup");//获取工作表中存在的行数int totalNoOfRows = sh.getRows();//获取工作表中存在的列数int totalNoOfCols = sh.getColumns();列表<字符串>list=new ArrayList();for (int row = 1; row < totalNoOfRows; row++) {for (int col = 1; col < totalNoOfCols; col++) {//System.out.print(sh.getCell(col, row).getContents() + "\t");list.add(sh.getCell(col, row).getContents());//return linemapper.mapLine(sh.getCell(col, row).getContents(), 0);}}退货清单;
我不明白如何与读者映射?
您可以使用 Apache POI 库读取 .xls 或 .xlsx
对于 Maven 项目:将以下依赖项添加到项目的 pom.xml 文件中:
<依赖><groupId>org.apache.poi</groupId><artifactId>poi</artifactId><version>2.6.12</version></依赖><!--对于 Excel 2007 格式 (XSSF) --><依赖><groupId>org.apache.poi</groupId><artifactId>poi-ooxml</artifactId><version>3.14</version></依赖>
这是使用 XSSF 的示例代码<预><代码>导入 java.io.File;导入 java.io.FileInputStream;导入 java.io.IOException;导入 org.apache.poi.ss.usermodel.Cell;导入 org.apache.poi.ss.usermodel.Row;导入 org.apache.poi.ss.usermodel.Sheet;导入 org.apache.poi.ss.usermodel.Workbook;导入 org.apache.poi.util.IOUtils;导入 org.apache.poi.xssf.usermodel.XSSFWorkbook;
I want to convert excel to xml.Can any one tell me how to proceed with mapper? My code:
public class WorkGroupReader implements ItemReader<List<String>> {
WorkGroupMapper linemapper;
public void setLinemapper(WorkGroupMapper linemapper) {
this.linemapper = linemapper;
}
@Override
public List<String> read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException,BiffException, IOException {
String FilePath = "E:\\ide-workspaces\\OmniFeed\\input\\WorkGroup.xls";
FileInputStream fs = new FileInputStream(FilePath);
Workbook wb = Workbook.getWorkbook(fs);
// TO get the access to the sheet
Sheet sh = wb.getSheet("WorkGroup");
// To get the number of rows present in sheet
int totalNoOfRows = sh.getRows();
// To get the number of columns present in sheet
int totalNoOfCols = sh.getColumns();
List<String> list=new ArrayList<String>();
for (int row = 1; row < totalNoOfRows; row++) {
for (int col = 1; col < totalNoOfCols; col++) {
//System.out.print(sh.getCell(col, row).getContents() + "\t");
list.add(sh.getCell(col, row).getContents());
//return linemapper.mapLine(sh.getCell(col, row).getContents(), 0);
}
}
return list;
i do not understand how to map with reader?
You can use Apache POI library to read .xls or .xlsx
For Maven projects: Add the following dependency to your project’s pom.xml file:
<!--For Excel 2003 format only (HSSF) -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>2.6.12</version>
</dependency>
<!--For Excel 2007 format (XSSF) -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
Here is an example code using XSSF
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelToXml {
public static void main(String[] args) {
File excelFile = new File(args[0]);
if (excelFile.exists()) {
Workbook workbook = null;
FileInputStream inputStream = null;
try {
inputStream = new FileInputStream(excelFile);
workbook = new XSSFWorkbook(inputStream);
Sheet sheet = workbook.getSheetAt(0);
for (Row row : sheet) {
for (Cell cell : row) {
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
System.out.print(cell.getStringCellValue());
break;
case Cell.CELL_TYPE_BOOLEAN:
System.out.print(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_NUMERIC:
System.out.print(cell.getNumericCellValue());
break;
}
System.out.print("\t");
}
System.out.println();
}
} catch (IOException e) {
// handle the exception
} finally {
IOUtils.closeQuietly(workbook);
IOUtils.closeQuietly(inputStream);
}
}
}
}
这篇关于我想将excel转换为xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!