java读取txt和excel赋值到二维数组的问题。
importjava.io.FileInputStream;importjava.io.FileNotFoundException;importjava.io.IOExc...
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class readtxt
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
List<String> list = new ArrayList<String>();
String filePath = "C:/a.xls";
InputStream fs = null;
Workbook workBook = null;
try {
fs = new FileInputStream(filePath);
workBook = Workbook.getWorkbook(fs);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
for (int n=0;n<3;n++){
Sheet sheet = workBook.getSheet(n);
Cell cell = null;
for (int j = 3; j < sheet.getRows(); j++) {
StringBuffer sb = new StringBuffer();
for (int i = 3; i <6; i++) {
cell = sheet.getCell(i, j);
sb.append(cell.getContents());
sb.append("|");
}
list.add(sb.toString());
}
}
workBook.close();
for(String ss:list){
System.out.println(ss);
}
}
java读取到了excel文件4-6列3列..怎么把其中的两列(第四第六列)赋值给一个二维数组。非常感谢~ 展开
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class readtxt
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
List<String> list = new ArrayList<String>();
String filePath = "C:/a.xls";
InputStream fs = null;
Workbook workBook = null;
try {
fs = new FileInputStream(filePath);
workBook = Workbook.getWorkbook(fs);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
for (int n=0;n<3;n++){
Sheet sheet = workBook.getSheet(n);
Cell cell = null;
for (int j = 3; j < sheet.getRows(); j++) {
StringBuffer sb = new StringBuffer();
for (int i = 3; i <6; i++) {
cell = sheet.getCell(i, j);
sb.append(cell.getContents());
sb.append("|");
}
list.add(sb.toString());
}
}
workBook.close();
for(String ss:list){
System.out.println(ss);
}
}
java读取到了excel文件4-6列3列..怎么把其中的两列(第四第六列)赋值给一个二维数组。非常感谢~ 展开
展开全部
根据你的代码改了下,输出没问题。
public class ReadTxt {
public static void main(String[] args) {
String filePath = "C:/1.xls";
InputStream fs = null;
Workbook workBook = null;
String[][] array = null;
try {
fs = new FileInputStream(filePath);
workBook = Workbook.getWorkbook(fs);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
int col = 0;
int row = 0;
Sheet[] sheets = workBook.getSheets();//获取sheet总数
for (int n = 0; n < sheets.length; n++) {
Sheet sheet = sheets[n];
if (sheet.getRows() == 0) {//如果没数据则跳过
continue;
}
array = new String[sheet.getRows() - 3][2];
Cell cell = null;
for (int j = 3; j < sheet.getRows(); j++) {
row = 0;
for (int i = 3; i < 6; i++) {
cell = sheet.getCell(i, j);
if (i != 4) {
array[col][row] = cell.getContents();
row++;
}
}
col++;
}
}
workBook.close();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
System.out.println("array[" + i + "," + j + "]" + array[i][j]);
}
}
}
}
public class ReadTxt {
public static void main(String[] args) {
String filePath = "C:/1.xls";
InputStream fs = null;
Workbook workBook = null;
String[][] array = null;
try {
fs = new FileInputStream(filePath);
workBook = Workbook.getWorkbook(fs);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
int col = 0;
int row = 0;
Sheet[] sheets = workBook.getSheets();//获取sheet总数
for (int n = 0; n < sheets.length; n++) {
Sheet sheet = sheets[n];
if (sheet.getRows() == 0) {//如果没数据则跳过
continue;
}
array = new String[sheet.getRows() - 3][2];
Cell cell = null;
for (int j = 3; j < sheet.getRows(); j++) {
row = 0;
for (int i = 3; i < 6; i++) {
cell = sheet.getCell(i, j);
if (i != 4) {
array[col][row] = cell.getContents();
row++;
}
}
col++;
}
}
workBook.close();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
System.out.println("array[" + i + "," + j + "]" + array[i][j]);
}
}
}
}
追问
array[i][j]里面的j数字一直在叠加 显示出来的数据是array[0][0]array[0][1]和array[1][2]array[1][3]。希望是array[1][0]和[1][1]怎么办呢?谢谢~
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
根据你的代码改了下,输出没问题。
public class ReadTxt {
public static void main(String[] args) {
String filePath = "C:/1.xls";
InputStream fs = null;
Workbook workBook = null;
String[][] array = null;
try {
fs = new FileInputStream(filePath);
workBook = Workbook.getWorkbook(fs);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
int col = 0;
int row = 0;
Sheet[] sheets = workBook.getSheets();//获取sheet总数
for (int n = 0; n < sheets.length; n++) {
Sheet sheet = sheets[n];
if (sheet.getRows() == 0) {//如果没数据则跳过
continue;
}
array = new String[sheet.getRows() - 3][2];
Cell cell = null;
for (int j = 3; j < sheet.getRows(); j++) {
row = 0;
for (int i = 3; i < 6; i++) {
cell = sheet.getCell(i, j);
if (i != 4) {
array[col][row] = cell.getContents();
row++;
}
}
col++;
}
}
workBook.close();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
System.out.println("array[" + i + "," + j + "]" + array[i][j]);
}
}
}
}
public class ReadTxt {
public static void main(String[] args) {
String filePath = "C:/1.xls";
InputStream fs = null;
Workbook workBook = null;
String[][] array = null;
try {
fs = new FileInputStream(filePath);
workBook = Workbook.getWorkbook(fs);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
int col = 0;
int row = 0;
Sheet[] sheets = workBook.getSheets();//获取sheet总数
for (int n = 0; n < sheets.length; n++) {
Sheet sheet = sheets[n];
if (sheet.getRows() == 0) {//如果没数据则跳过
continue;
}
array = new String[sheet.getRows() - 3][2];
Cell cell = null;
for (int j = 3; j < sheet.getRows(); j++) {
row = 0;
for (int i = 3; i < 6; i++) {
cell = sheet.getCell(i, j);
if (i != 4) {
array[col][row] = cell.getContents();
row++;
}
}
col++;
}
}
workBook.close();
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 2; j++) {
System.out.println("array[" + i + "," + j + "]" + array[i][j]);
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询