发布网友 发布时间:2022-04-23 23:14
共2个回答
热心网友 时间:2023-09-18 07:26
使用java poi
package webservice;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
public class ExcelController {
@SuppressWarnings("deprecation")
public void excel() throws FileNotFoundException, IOException{
String filename = "d:\\excel.xls";
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filename));
//按名引用excel工作表
// HSSFSheet sheet = workbook.getSheet("JSP");
//用式获取excel工作表采用工作表索引值
HSSFSheet sheet = workbook.getSheetAt(0);
HSSFRow row ;
HSSFCell cell1;
int rows=sheet.getLastRowNum();
for(int icount=0;icount<rows;icount++){
row = sheet.getRow(icount);
int line=row.getPhysicalNumberOfCells();
for(int j=0;j<line;j++){
cell1= row.getCell(j);
System.out.println(cell1+"--"+icount+"---"+j);
}
}
//打印读取值
// System.out.println(cell.getStringCellValue());
//新建输流
FileOutputStream fout = new FileOutputStream(filename); //PS:filename 另存路径处理直接写入模版文件
//存盘
workbook.write(fout);
fout.flush();
//结束关闭
fout.close();
}
public HSSFCell getCell(HSSFRow row, int index) {
// 取发期单元格
HSSFCell cell = row.getCell(index);
// 单元格存
if (cell == null) {
// 创建单元格
cell = row.createCell(index);
}
// 返单元格
return cell;
}
public static void main(String[] args) {
ExcelController ec = new ExcelController();
try {
ec.excel();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
热心网友 时间:2023-09-18 07:27
添加spire.xls.jar为依赖,使用下面的代码
import com.spire.xls.*;
public class ReadExcel {
public static void main(String[] args) {
//创建Workbook对象
Workbook wb = new Workbook();
//加载一个Excel文档
wb.loadFromFile("C:\\Users\\Administrator\\Desktop\\test.xlsx");
//获取第一个工作表
Worksheet sheet = wb.getWorksheets().get(0);
//遍历工作表的每一行
for (int i = 1; i < sheet.getLastRow() + 1; i++) {
//遍历工作的每一列
for (int j = 1; j < sheet.getLastColumn() + 1; j++) {
//输出指定单元格的数据
System.out.print(sheet.get(i,j).getText());
System.out.print("\t");
}
System.out.print("\n");
}
}
}