我想将excel转换为xml [英] I want to convert excel to xml

查看:38
本文介绍了我想将excel转换为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;

公共类 ExcelToXml {公共静态无效主(字符串 [] args){文件 excelFile = new File(args[0]);如果(excelFile.exists()){工作簿工作簿=空;FileInputStream inputStream = null;尝试 {inputStream = new FileInputStream(excelFile);工作簿 = 新 XSSFWorkbook(inputStream);Sheet sheet = workbook.getSheetAt(0);for (Row row : sheet) {对于(单元格单元格:行){开关(cell.getCellType()){案例 Cell.CELL_TYPE_STRING:System.out.print(cell.getStringCellValue());休息;案例 Cell.CELL_TYPE_BOOLEAN:System.out.print(cell.getBooleanCellValue());休息;案例 Cell.CELL_TYPE_NUMERIC:System.out.print(cell.getNumericCellValue());休息;}System.out.print("\t");}System.out.println();}} catch (IOException e) {//处理异常} 最后 {IOUtils.closeQuietly(工作簿);IOUtils.closeQuietly(inputStream);}}}}

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆