java excel xml_如何使用java将Excel转换为XML?

5 个答案:

答案 0 :(得分:6)

查看jexcel或Apache POI库以便在Excel文件中读取。

创建XML文件很简单,只需将XML直接写入文件,或者附加到XML文档,然后使用标准Java库或Xerces或类似文件将其写出来。

答案 1 :(得分:4)

我最近在Java中将Excel(xlsx)转换为xml。我假设excel中的每一行都是一个单独的对象。以下是我遵循的步骤: -

使用Apache POI阅读Excel文件

创建了一个xsd文件

生成相应的类

使用类中生成的getter / setter方法读取创建的每一行,创建相应的对象和初始值

将对象添加到只包含相同类型对象的arraylist

使用Jaxb将arraylist对象标记为输出文件

醇>

答案 2 :(得分:2)

JExcel对我来说很容易使用。将jxl.jar放在类路径上并编写如下代码:

File excelFile = new File(excelFilename);

// Create model for excel file

if (excelFile.exists()) {

try {

Workbook workbook = Workbook.getWorkbook(excelFile);

Sheet sheet = workbook.getSheets()[0];

TableModel model = new DefaultTableModel(sheet.getRows(), sheet.getColumns());

for (int row = 0; row < sheet.getRows(); row++) {

for (int column = 0; column < sheet.getColumns(); column++) {

String content = sheet.getCell(column, row).getContents();

model.setValueAt(content, row, column);

}

}

previewTable.setModel(model);

} catch (Exception e) {

JOptionPane.showMessageDialog(null, "Error: " + e);

}

} else {

JOptionPane.showMessageDialog(null, "File does not exist");

}

答案 3 :(得分:1)

File excelFile = new File(excelFilename);

// Create model for excel file

if (excelFile.exists()) {

try {

Workbook workbook = Workbook.getWorkbook(excelFile);

Sheet sheet = workbook.getSheets()[0];

TableModel model = new DefaultTableModel(sheet.getRows(), sheet.getColumns());

for (int row = 0; row < sheet.getRows(); row++) {

for (int column = 0; column < sheet.getColumns(); column++) {

String content = sheet.getCell(column, row).getContents();

model.setValueAt(content, row, column);

}

}

previewTable.setModel(model);

} catch (Exception e) {

JOptionPane.showMessageDialog(null, "Error: " + e);

}

} else {

JOptionPane.showMessageDialog(null, "File does not exist");

}

答案 4 :(得分:0)

下载jxl并使用此代码

import java.io.File;

import java.io.IOException;

import java.io.UnsupportedEncodingException;

import javax.swing.text.BadLocationException;

import jxl.Cell;

import jxl.CellType;

import jxl.Sheet;

import jxl.Workbook;

import jxl.format.Font;

import jxl.read.biff.BiffException;

public class XlsToXml {

public String toXml(File excelFile) throws IOException, BiffException {

try {

String xmlLine = "";

String rowText = "";

String colText = "";

String isBold = "";

Font font = null;

String cellCol = "";

String cellAddress = "";

Cell cell = null;

Workbook workbook = Workbook.getWorkbook(excelFile);

xmlLine += "" + "\n";

for (int sheet = 0; sheet < workbook.getNumberOfSheets(); sheet++) {

Sheet s = workbook.getSheet(sheet);

xmlLine += " " + "\n";

Cell[] row = null;

for (int i = 0; i < s.getRows(); i++) {

row = s.getRow(i);

for (int j = 0; j < row.length; j++) {

if (row[j].getType() != CellType.EMPTY) {

cell = row[j];

cellCol=columnName(cell.getColumn());

cellCol=" colLetter=\""+cellCol+"\"";

cellAddress=" address=\""+cellAddress(cell.getRow()+1,cell.getColumn())+"\"";

isBold = cell.getCellFormat().getFont().getBoldWeight() == 700 ? "true" : "false";

isBold = (isBold == "false" ? "" : " isBold=\"true\"");

colText += "

";

colText += "";

colText += "" + "\n";

rowText += cell.getContents();

}

}

if (rowText != "") {

xmlLine += " " + "\n";

xmlLine += colText;

xmlLine += " " + "\n";

}

colText = "";

rowText = "";

}

xmlLine += " " + "\n";;

}

xmlLine += "";

return xmlLine;

} catch (UnsupportedEncodingException e) {

System.err.println(e.toString());

}

return null;

}

private String cellAddress(Integer rowNumber, Integer colNumber){

//return "$"+columnName(colNumber)+"$"+rowNumber;

return columnName(colNumber)+rowNumber;

}

private String columnName(Integer colNumber) {

Base columns = new Base(colNumber,26);

columns.transform();

return columns.getResult();

}

class Base {

String[] colNames = "A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z".split(",");

String equalTo;

int position;

int number;

int base;

int[] digits;

int[] auxiliar;

public Base(int n, int b) {

position = 0;

equalTo = "";

base = b;

number = n;

digits = new int[1];

}

public void transform() {

if (number < base) {

digits[position] = number;

size();

} else {

digits[position] = number % base;

size();

position++;

number = number / base;

transform();

}

}

public String getResult() {

for (int j = digits.length - 2; j >= 0; j--) {

equalTo += colNames[j>0?digits[j]-1:digits[j]];

}

return equalTo;

}

private void size() {

auxiliar = digits;

digits = new int[auxiliar.length + 1];

System.arraycopy(auxiliar, 0, digits, 0, auxiliar.length);

}

}

}

  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:数字20 设计师:CSDN官方博客 返回首页
评论

打赏作者

余昌黔

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值