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();
		}
	}
}

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用 Apache POI 库来实现 Java Excel 生成 JSON 数据的功能。下面是一个简单的示例代码: ```java import org.apache.poi.ss.usermodel.*; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.json.JSONArray; import org.json.JSONObject; import java.io.FileInputStream; import java.io.IOException; public class ExcelToJsonConverter { public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("path_to_excel_file.xlsx"); Workbook workbook = new XSSFWorkbook(fis); Sheet sheet = workbook.getSheetAt(0); JSONArray jsonArray = new JSONArray(); for (Row row : sheet) { JSONObject jsonObject = new JSONObject(); for (Cell cell : row) { String columnName = sheet.getRow(0).getCell(cell.getColumnIndex()).getStringCellValue(); jsonObject.put(columnName, getCellValue(cell)); } jsonArray.put(jsonObject); } System.out.println(jsonArray.toString()); workbook.close(); fis.close(); } catch (IOException e) { e.printStackTrace(); } } private static Object getCellValue(Cell cell) { switch (cell.getCellType()) { case STRING: return cell.getStringCellValue(); case NUMERIC: if (DateUtil.isCellDateFormatted(cell)) { return cell.getDateCellValue(); } else { return cell.getNumericCellValue(); } case BOOLEAN: return cell.getBooleanCellValue(); case FORMULA: return cell.getCellFormula(); default: return null; } } } ``` 请将 `path_to_excel_file.xlsx` 替换为实际的 Excel 文件路径。该示例将 Excel 文件中的数据读取并换为 JSON 数组,并打印输出。你可以根据需要对输出进行进一步处理。

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

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值