Java如何将二维数组写入excel
展开全部
java读取txt文件然后赋值二维数组实现方法如下:
package shi;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Test13 {
/**
* 读取文件
* @param filePath
* @return
*/
public static List readTxtFile(String filePath) {
List<String> list = new ArrayList<String>();
try {
String encoding = "UTF-8";
File file = new File(filePath);
if (file.isFile() && file.exists()) {
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
if (!lineTxt.startsWith("#"))
list.add(lineTxt);
}
read.close();
} else {
System.out.println("找不到文件");
}
} catch (Exception e) {
System.out.println("出错了");
e.printStackTrace();
}
return list;
}
/**
* 创建二维数组
* @param list
* @return
*/
public static String[][] createArray(String filePath){
List<String> list = readTxtFile(filePath);
String array[][] = new String[list.size()][];
for(int i=0;i<list.size();i++){
array[i] = new String[3];
String linetxt=list.get(i);
String[] myArray = linetxt.replaceAll("\\s+", "@").split("@");
for(int j=0;j<myArray.length;j++){
if(j<3){
array[i][j]=myArray[j];
}
}
}
return array;
}
/**
* 打印数组
* @param array
*/
public static void printArray(String array[][]){
for(int i=0;i<array.length;i++){
for(int j=0;j<array[i].length;j++){
if(j!=array[i].length-1){
System.out.print("array["+i+"]["+j+"]="+array[i][j]+",");
}
else{
System.out.print("array["+i+"]["+j+"]="+array[i][j]);
}
}
System.out.println();
}
}
public static void main(String args[]) {
String array[][] = createArray("F:\\test1.txt");
printArray(array);
}
}
package shi;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class Test13 {
/**
* 读取文件
* @param filePath
* @return
*/
public static List readTxtFile(String filePath) {
List<String> list = new ArrayList<String>();
try {
String encoding = "UTF-8";
File file = new File(filePath);
if (file.isFile() && file.exists()) {
InputStreamReader read = new InputStreamReader(
new FileInputStream(file), encoding);
BufferedReader bufferedReader = new BufferedReader(read);
String lineTxt = null;
while ((lineTxt = bufferedReader.readLine()) != null) {
if (!lineTxt.startsWith("#"))
list.add(lineTxt);
}
read.close();
} else {
System.out.println("找不到文件");
}
} catch (Exception e) {
System.out.println("出错了");
e.printStackTrace();
}
return list;
}
/**
* 创建二维数组
* @param list
* @return
*/
public static String[][] createArray(String filePath){
List<String> list = readTxtFile(filePath);
String array[][] = new String[list.size()][];
for(int i=0;i<list.size();i++){
array[i] = new String[3];
String linetxt=list.get(i);
String[] myArray = linetxt.replaceAll("\\s+", "@").split("@");
for(int j=0;j<myArray.length;j++){
if(j<3){
array[i][j]=myArray[j];
}
}
}
return array;
}
/**
* 打印数组
* @param array
*/
public static void printArray(String array[][]){
for(int i=0;i<array.length;i++){
for(int j=0;j<array[i].length;j++){
if(j!=array[i].length-1){
System.out.print("array["+i+"]["+j+"]="+array[i][j]+",");
}
else{
System.out.print("array["+i+"]["+j+"]="+array[i][j]);
}
}
System.out.println();
}
}
public static void main(String args[]) {
String array[][] = createArray("F:\\test1.txt");
printArray(array);
}
}
展开全部
在java程序中添加Spire.Xls.jar为依赖
import com.spire.xls.*;
public class CreateExcel {
public static void main(String[] args) throws Exception {
//创建Workbook对象
Workbook wb = new Workbook();
//获取第一张工作表
Worksheet sheet = wb.getWorksheets().get(0);
//定义一维数据
String[] oneDimensionaArray = new String[]{"Apple", "Pear", "Grape", "Banana", "Peach"};
//从指定单个格开始写入工作表,true表示纵向写入,设置为false为横向写入
sheet.insertArray(oneDimensionaArray, 1, 1, true);
//定义二维数组
String[][] twoDimensionalArray = new String[][]{
{"姓名", "年龄", "性别", "学历"},
{"小王", "25", "男", "本科"},
{"小李", "24", "女", "本科"},
{"小张", "26", "男", "大专"}
};
//从指定单元格开始写入二维数组到工作表
sheet.insertArray(twoDimensionalArray, 1, 3);
//保存文档
wb.saveToFile("output/InsertData.xlsx", ExcelVersion.Version2016);
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
呼呼呼呼呼呼呼呼呼胡滚滚滚滚滚滚滚滚滚胡
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询