我想将 excel 转换为 xml
I want to convert excel to xml
我想将 excel 转换为 xml.Can 谁能告诉我如何使用映射器?
我的代码:
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;
我不明白如何用 reader 映射?
i do not understand how to map with reader?
您没有映射 reader。
你把 / 作为内存中的数据结构读入,然后写出数据结构为 XML。假设这就是您要在此处执行的操作。
(您也可以逐步完成,但不要让事情复杂化。)
您可以使用 Apache POI 库来读取 .xls 或 .xlsx
对于 Maven 项目:将以下依赖项添加到项目的 pom.xml 文件中:
<!--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>
这是使用 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;</p>
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.Can 谁能告诉我如何使用映射器? 我的代码:
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;
我不明白如何用 reader 映射?
i do not understand how to map with reader?
您没有映射 reader。
你把 / 作为内存中的数据结构读入,然后写出数据结构为 XML。假设这就是您要在此处执行的操作。
(您也可以逐步完成,但不要让事情复杂化。)
您可以使用 Apache POI 库来读取 .xls 或 .xlsx
对于 Maven 项目:将以下依赖项添加到项目的 pom.xml 文件中:
<!--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>
这是使用 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;</p>
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);
}
}
}
}