java将excel转成json,java excel转json

package com.xmg.excel;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStreamWriter;

import java.net.URL;

import com.xmg.constants.ExcelConstant;

import jxl.Cell;

import jxl.Sheet;

import jxl.Workbook;

import jxl.read.biff.BiffException;

import net.sf.json.JSONArray;

public class ExcelTranslator {

public static void main(String[] args) {

String cfgFile = "test.xls";

if (args.length > 0) {

cfgFile = args[0];

}

new ExcelTranslator().translateFile(cfgFile);

//new ExcelTranslator().test();

}

private void test() {

JSONArray array1 = new JSONArray();

array1.add(12);

array1.add(22);

JSONArray array2 = new JSONArray();

array2.add(21);

array2.add(22);

JSONArray array = new JSONArray();

array.add(array1);

array.add(array2);

System.out.println(array.toString());

}

Sheet sheet;

Workbook book;

private void translateFile(String cfgFile) {

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();

URL url = classLoader.getResource(cfgFile);

try {

book = Workbook.getWorkbook(new File(url.getFile()));

sheet = book.getSheet(0);

outputToJSON(sheet);

} catch (BiffException | IOException e) {

e.printStackTrace();

}

}

private void outputToJSON(Sheet sheet2) throws IOException {

JSONArray arr = new JSONArray();

for (int i = ExcelConstant.DATA_START_INDEX; i < sheet.getRows(); i++) {

JSONArray rowarr = new JSONArray();

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

Cell cell = sheet.getCell(j, i);

rowarr.add(cell.getContents());

}

arr.add(rowarr);

}

writeToFile(arr.toString(),".json");

}

public void outputToTxt(Sheet sheet) throws IOException {

StringBuilder builder = new StringBuilder();

for (int i = ExcelConstant.DATA_START_INDEX; i < sheet.getRows(); i++) {

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

Cell cell = sheet.getCell(j, i);

builder.append(cell.getContents() + "|");

}

builder.append("\r\n");

}

writeToFile(builder.toString(),".txt");

}

public void writeToFile(String info,String filePostfix) throws IOException {

String path = ExcelConstant.GENERATE_TXT_LOCATION + sheet.getName() + filePostfix;

BufferedWriter bw = null;

try {

FileOutputStream out = new FileOutputStream(path, true);// true,表示:文件追加内容,不重新生成,默认为false

bw = new BufferedWriter(new OutputStreamWriter(out, "GBK"));

bw.write(info += "\r\n");// 换行

bw.flush();

} catch (Exception e) {

e.printStackTrace();

} finally {

bw.close();

}

}

public void printOutContent(Sheet sheet) {

for (int i = ExcelConstant.DATA_START_INDEX; i < sheet.getRows(); i++) {

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

Cell cell = sheet.getCell(j, i);

System.out.print(cell.getContents() + "|");

}

System.out.println();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值