java excel转json

java excel转jsonpackagecom.xmg.excel;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileOutputStream;importjava.io.IOException;importjava.io.OutputStreamWriter;importjava.net.URL;import…

大家好,又见面了,我是你们的朋友全栈君。

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

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/130771.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 做java开发的前景何在

    做java开发的前景何在做了几年的it研发工程师,到现在还从事coding,尤其我本身是女生,工作也有5年多了,一直从事coding,用过很多的开发语言,但是好像没有精通。所以找工作不知道该用哪种语言找,于是乎瞎碰,没有目标的乱碰。呵呵。曲折的工作经历,中间休息了两年没有开发,搞了半年的需求分析,后来由于不喜欢新的工作环境离职,现在又走上了老路coding,本身我觉得没有什么,但是在现在的公司里越来越觉得做研发尤其是…

    2022年7月14日
    13
  • ubuntu 20.04.1 LTS_visi20安装教程

    ubuntu 20.04.1 LTS_visi20安装教程UbuntuServer20.04LTS下载及安装教程

    2022年9月8日
    0
  • Python画图显示中文

    Python画图显示中文matplotlib作图时默认设置下为英文,无法显示中文,只需要添加下面两行代码即可plt.rcParams[‘font.sans-serif’]=[‘SimHei’]plt.rcParams[‘axes.unicode_minus’]=FalseExampleimportmatplotlib.pyplotaspltfromnumpy.randomimportmul…

    2022年6月1日
    37
  • python官网下载步骤-windows下载并安装Python的具体步骤

    python官网下载步骤-windows下载并安装Python的具体步骤安装Python下载并安装PythonPython的官网是www.python.org,我们可以直接从官网下载Python。这里介绍在微软Windows和苹果MacOS两种系统中的安装方式。如果Python官网页面之后有所更新,那请大家用自己的思维能力和观察力,大胆地尝试,推测如何下载安装,解决问题。1.5.1Windows系统进入https://www.python.org/页面,选…

    2022年5月1日
    86
  • FVWM简明使用指南 pdf_fpv教程

    FVWM简明使用指南 pdf_fpv教程http://docs.huihoo.com/homepage/shredderyin/fvwm.html FVWM简明使用指南FvwmisawindowmanagerforX11.Itisdesignedtominimizememoryconsumption,providea3Dlooktowindowframes,andavirtual

    2022年10月3日
    0
  • web网页设计代码模板_web网页设计源代码

    web网页设计代码模板_web网页设计源代码代码分享:https://pan.baidu.com/s/1xf4AQlfM8e6Nvw2oXUdbrA提取码:ap1v

    2022年9月23日
    1

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号