java 将一个数组导出 txt 文件或 excel 里面的一个简单方法

网上搜了不少方法,java 将数据读取到excel 里面非常麻烦,添加第三方包,还要定义表头,定义工作簿,单元格,并且要一个单元格一个单元格读取。

自己又在国外网站搜了一个简单的方法:就像导出 txt 文件那样导出到 excel,只不过把文件名后缀改为 xls,打开文件时用 excel 打开。

举例:

import java.io.FileWriter;
import java.io.IOException;

/** 
* @author chen zhen 
* @version 创建时间:2018年4月13日 下午6:30:11 
* @value 类说明: 将一个数组读取到 txt 里面或 excel 里
*
*/
public class WriteArray {
	
	public void writeArrayToTxt(double[][] data, String string) {
		int rowNum = data.length;
		int columnNum = data[0].length;
		try {
			FileWriter fw = new FileWriter(string);
			for (int i = 0; i < rowNum; i++) {
				for (int j = 0; j < columnNum; j++)
					fw.write(data[i][j]+ "\t");
				fw.write("\n");
			}
			fw.close();
		}
		catch (IOException e){
			e.printStackTrace();
		}		
	}
	
	// 导出到 excel的代码其实跟导出到 txt 的代码一样
	public void writeArrayToExcel(double[][] data, String string) {
		int rowNum = data.length;
		int columnNum = data[0].length;
		try {
			FileWriter fw = new FileWriter(string);
			for (int i = 0; i < rowNum; i++) {
				for (int j = 0; j < columnNum; j++)
					fw.write(data[i][j]+ "\t"); // tab 间隔
				fw.write("\n"); // 换行
			}
			fw.close();
		}
		catch (IOException e){
			e.printStackTrace();
		}
		
	}
	
	
	public static void main(String[] args) {

		 double[][] demands = {{7,7,7,7,7,7},
                 {2,3,4,5,6,7},
                 {8,7,6,5,4,3},
                 {5,6,7,8,7,6},
                 {8,5,2,1,2,5},
                 {8,4,1,3,1,3},
                 {1,3,8,4,8,7},
                 {1,4,7,3,5,8},
                 {3,8,4,4,6,2},
                 {3,1,5,8,4,4}
                 };
		 
		 WriteArray wa = new WriteArray();
		 wa.writeArrayToTxt(demands, "mytxt.txt");
		 wa.writeArrayToTxt(demands, "mytxt.xls");
	}
}
  • 9
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 12
    评论
好的,以下是一个简单Java 代码示例,用于将数据导出Excel 文件中: ```java import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ExcelExporter { public static void main(String[] args) { // 创建工作簿对象 XSSFWorkbook workbook = new XSSFWorkbook(); // 创建工作表对象 XSSFSheet sheet = workbook.createSheet("Sheet1"); // 创建数据 Object[][] data = { {"姓名", "年龄", "性别"}, {"张三", 20, "男"}, {"李四", 25, "女"}, {"王五", 30, "男"}, {"赵六", 35, "女"} }; // 写入数据 int rowNum = 0; for (Object[] rowData : data) { Row row = sheet.createRow(rowNum++); int colNum = 0; for (Object field : rowData) { Cell cell = row.createCell(colNum++); if (field instanceof String) { cell.setCellValue((String) field); } else if (field instanceof Integer) { cell.setCellValue((Integer) field); } } } // 保存文件 try (FileOutputStream outputStream = new FileOutputStream("data.xlsx")) { workbook.write(outputStream); } catch (IOException e) { e.printStackTrace(); } System.out.println("导出成功!"); } } ``` 在这个示例中,我们使用 Apache POI 库创建了一个 XSSFWorkbook 对象,并将其写入 Excel 文件中。首先,创建了一个工作表对象,然后创建数据数组并将其写入工作表中。最后,使用 `FileOutputStream` 将工作簿对象写入文件中。注意,需要在 try-with-resources 语句中打开文件输出流,以确保在写入数据后关闭文件
评论 12
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

心态与习惯

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

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

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

打赏作者

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

抵扣说明:

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

余额充值