JAVA日报

excel(exceldao)

package dao;

import bean.item;

import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;


public class ExcelDao {
public static ArrayList<item> getAlldatainfos() throws SQLException, BiffException, IOException {
ArrayList<item> ds=new ArrayList<>();
File xlsFile = new File("test.xls");
// 获得工作簿对象
Workbook workbook = Workbook.getWorkbook(xlsFile);
// 获得所有工作表
Sheet[] sheets = workbook.getSheets();
// 遍历工作表
if (sheets != null)
{
for (Sheet sheet : sheets)
{
// 获得行数
int rows = sheet.getRows();
// 获得列数
int cols = sheet.getColumns();
// 读取数据
for (int row = 0; row < rows; row++)
{
item d=new item();
d.setContent(sheet.getCell(0, row).getContents());
d.setChannelName(sheet.getCell(1, row).getContents());
d.setTitle(sheet.getCell(2, row).getContents());
// for (int col = 0; col < cols; col++)
// {
// System.out.printf("%10s", sheet.getCell(col, row).getContents());
// }
// System.out.println();
System.out.println(d.getContent());
ds.add(d);
}
}
}
workbook.close();
return ds;
}

// public static void main(String[] args) throws BiffException, SQLException, IOException {
// ArrayList thing = new ExcelDao().getAlldatainfos();
// System.out.println(thing.get(1));
// }
}
posted @ 2021-12-09 21:32  我的未来姓栗山  阅读(50)  评论(0编辑  收藏  举报