使用JAVA从Excel获取数据并将行数打印到Excel中
嗨,我有一张上面这样的Excel表格。我需要计算所有流行路线,并在相应的S_No行的No.of-routes列中打印这些路线。而且我在每个序号后面都有一个空行。而且这些所有流行的路线都不是放在一个单元格中(每条路线是一行)。 我尝试了以下代码。我不能前进了,请帮帮我使用JAVA从Excel获取数据并将行数打印到Excel中,java,excel,Java,Excel,嗨,我有一张上面这样的Excel表格。我需要计算所有流行路线,并在相应的S_No行的No.of-routes列中打印这些路线。而且我在每个序号后面都有一个空行。而且这些所有流行的路线都不是放在一个单元格中(每条路线是一行)。 我尝试了以下代码。我不能前进了,请帮帮我 S_No Operators About No.Of-Busses Main-Routes No.Of-Routes Popular-Routes 1 A-G-Holidays ***
S_No Operators About No.Of-Busses Main-Routes No.Of-Routes Popular-Routes
1 A-G-Holidays *** 10 Delhi - Haridwar Delhi - Haridwar
Delhi - Dehradun
Delhi - Kanpur
Delhi - Lucknow
Delhi - Rishikesh
Rishikesh - Delhi
Kanpur - Lucknow
Haridwar - Delhi
Haridwar - Rishikesh
Haridwar - Dehradun
blank line-----------------------------------------------------------------------
2 A-K-Travels *** 2 0
Mumbai - Indore
Indore - Mumbai
公共类PrintNoOfRoutes
{
公共静态void main(字符串args[])引发异常
{
列表=新的ArrayList();
FileInputStream文件=新的FileInputStream(新文件(“D:/BusOperators/sample.xlsx”);
XSSF工作簿=新XSSF工作簿(文件);
XSSFSheet sheet=workbook.getSheetAt(0);
迭代器rowIterator=sheet.Iterator();
roweiterator.next();
行=行迭代器。下一步();
int S_No=(int)row.getCell(0.getNumericCellValue();
系统输出打印项次(序号);
迭代器cellIterator=row.cellIterator();
while(cellIterator.hasNext())
{
Cell=cellIterator.next();
开关(cell.getCellType())
{
case Cell.Cell\u类型\u布尔值:
System.out.println(“boolean===>>>”+cell.getBooleanCellValue()+“\t”);
打破
case Cell.Cell\u类型\u数值:
int S_No=(int)row.getCell(0.getNumericCellValue();
系统输出打印项次(序号);
打破
case Cell.Cell\u类型\u字符串:
//添加(cell.getStringCellValue());
如果(c==S_否)
{
系统输出打印项次(“完成”);
行。getCell(6);
行row2=行迭代器.next();
if(cell.getStringCellValue()!=null)
{
计数=1;
系统输出打印项次(计数);
计数++;
}
打破
}
这对我来说很有效。这取决于一个空行来指示何时停止计算特定S_编号的常用路线。S_编号和路线编号必须是数字字段
public class PrintNoOfRoutes
{
public static void main(String args[]) throws Exception
{
List list=new ArrayList();
FileInputStream file = new FileInputStream(new File("D:/BusOperators/sample.xlsx"));
XSSFWorkbook workbook = new XSSFWorkbook(file);
XSSFSheet sheet = workbook.getSheetAt(0);
Iterator<Row> rowIterator = sheet.iterator();
rowIterator.next();
Row row = rowIterator.next();
int S_No=(int) row.getCell(0).getNumericCellValue();
System.out.println(S_No);
Iterator<Cell> cellIterator = row.cellIterator();
while(cellIterator.hasNext())
{
Cell cell = cellIterator.next();
switch(cell.getCellType())
{
case Cell.CELL_TYPE_BOOLEAN:
System.out.println("boolean===>>>"+cell.getBooleanCellValue() + "\t");
break;
case Cell.CELL_TYPE_NUMERIC:
int S_No=(int) row.getCell(0).getNumericCellValue();
System.out.println(S_No);
break;
case Cell.CELL_TYPE_STRING:
//list.add(cell.getStringCellValue());
if(c==S_No)
{
System.out.println("done");
row.getCell(6);
Row row2 = rowIterator.next();
if(cell.getStringCellValue() != null)
{
count=1;
System.out.println(count);
count++;
}
break;
}
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.FileNotFoundException;
导入java.io.FileOutputStream;
导入java.io.IOException;
导入java.util.Iterator;
导入org.apache.poi.ss.usermodel.Cell;
导入org.apache.poi.ss.usermodel.CellType;
导入org.apache.poi.ss.usermodel.Row;
导入org.apache.poi.ss.usermodel.Sheet;
导入org.apache.poi.ss.usermodel.工作簿;
导入org.apache.poi.xssf.usermodel.xssf工作簿;
公共类PrintNoOfRoutes
{
私有静态最终字符串infle_NAME=“C:\\sample.xlsx”;
私有静态最终字符串输出文件\u NAME=“C:\\sampleout.xlsx”;
公共静态void main(字符串[]args){
试一试{
FileInputStream excelFile=newfileinputstream(新文件(infle_名称));
工作簿=新XSSF工作簿(Excel文件);
工作表数据类型工作表=工作簿。getSheetAt(0);
迭代器迭代器=datatypeSheet.Iterator();
布尔值firstTime=true;
int numRoutes=0;
Cell-routeCell=null;
while(iterator.hasNext()){
Row currentRow=iterator.next();
迭代器cellIterator=currentRow.Iterator();
while(cellIterator.hasNext()){
Cell currentCell=cellIterator.next();
如果(currentCell.getCellTypeEnum()==CellType.STRING&¤tCell.getColumnIndex()==6){
//找到了要计数的路径
String val=currentCell.getStringCellValue();
if(!firstTime)//不计算标题行
numRoutes++;
}else if(currentCell.getCellTypeEnum()==CellType.NUMERIC&¤tCell.getColumnIndex()==0){
//找到具有值的S_No单元格
如果(!第一次){
//设置上一个序列号的路由数
routeCell.setCellValue(numRoutes);
numRoutes=0;//重置
}否则{
第一次=错误;
}
routeCell=currentRow.getCell(5);//保存此序列号的路由编号单元格
}
}
}
//写入上次路由计数
routeCell.setCellValue(numRoutes);
//写出练习册
文件输出文件=新文件(输出文件名称);
FileOutputStream fos=新的FileOutputStream(输出文件);
练习册。写作(fos);
workbook.close();
fos.close();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
}
是空行实际上是空的,还是它包含此空行-------------------------------------------------------------------
?否。它是空的(空行)谢谢您的回复。需要其他罐子吗?我只是按照您的示例使用了相关的罐子。
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class PrintNoOfRoutes
{
private static final String INFILE_NAME = "C:\\sample.xlsx";
private static final String OUTFILE_NAME = "C:\\sampleout.xlsx";
public static void main(String[] args) {
try {
FileInputStream excelFile = new FileInputStream(new File(INFILE_NAME));
Workbook workbook = new XSSFWorkbook(excelFile);
Sheet datatypeSheet = workbook.getSheetAt(0);
Iterator<Row> iterator = datatypeSheet.iterator();
boolean firstTime = true;
int numRoutes = 0;
Cell routeCell = null;
while (iterator.hasNext()) {
Row currentRow = iterator.next();
Iterator<Cell> cellIterator = currentRow.iterator();
while (cellIterator.hasNext()) {
Cell currentCell = cellIterator.next();
if (currentCell.getCellTypeEnum() == CellType.STRING && currentCell.getColumnIndex() == 6) {
//found a route to count
String val = currentCell.getStringCellValue();
if (!firstTime) //don't count header row
numRoutes++;
} else if (currentCell.getCellTypeEnum() == CellType.NUMERIC && currentCell.getColumnIndex() == 0) {
//found an S_No cell with a value
if (!firstTime) {
//set the number of routes for the previous S_No
routeCell.setCellValue(numRoutes);
numRoutes = 0; //reset
} else {
firstTime = false;
}
routeCell = currentRow.getCell(5); //save the No.Of-Routes cell for this S_No
}
}
}
//write last route count
routeCell.setCellValue(numRoutes);
//write out the workbook
File outfile = new File(OUTFILE_NAME);
FileOutputStream fos = new FileOutputStream(outfile);
workbook.write(fos);
workbook.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}