java用excel表_在java中使用excel表格

我是二年级学生。我必须完成我的java项目,但如果你们中的任何人都可以帮忙,我会面临一些问题。我的项目是使用java在excel工作表中输入学生的标记并计算标记的平均值。我写了下面的代码,但它无法正常工作......在java中使用excel表格

这里是我的代码...

import java.util.*;

import org.apache.poi.hssf.usermodel.*;

import org.apache.poi.hssf.util.*;

import org.apache.poi.ss.usermodel.*;

import org.apache.poi.ss.util.*;

import java.io.*;

public class exceluser {

public static void main(String[] args){

String St_Id = "";

String Name = "";

int Marks = 0;

boolean keepRunning = true;

//Blank workbook

HSSFWorkbook workbook = new HSSFWorkbook();

//Blank sheet

HSSFSheet sheet = workbook.createSheet("Seating Details");

//create heading

Row rowHeading = sheet.createRow(0);

rowHeading.createCell(0).setCellValue("Student ID");

rowHeading.createCell(1).setCellValue("Student(s) Name");

rowHeading.createCell(2).setCellValue("Marks");

//Font size and style loop for my headers

for(int i = 0; i < 3; i++)

{

CellStyle stylerowHeading = workbook.createCellStyle();

Font font = workbook.createFont();

font.setBold(true);

font.setFontName(HSSFFont.FONT_ARIAL);

font.setFontHeightInPoints((short) 11);

stylerowHeading.setFont(font);

rowHeading.getCell(i).setCellStyle(stylerowHeading);

}

int rownum=1;

while(keepRunning){

Scanner scnr = new Scanner(System.in);

System.out.print("Enter student ID : ");

St_Id = scnr.nextLine();

if(St_Id.equals("EXIT"))

{

System.out.print("You have ended the program");

System.exit(0);

}

else

{

System.out.print("Enter the student name : ");

Name = scnr.nextLine();

System.out.print("Enter the marks of the student : ");

Marks = scnr.nextInt();

//This data needs to be written (Object[])

Map data = new TreeMap();

data.put("2", new Object[] {St_Id, Name, Marks});

//Iterate over data and write to sheet

Set keyset = data.keySet();

for(String Key : keyset)

{

Row row = sheet.createRow(rownum++);

Object [] objArr = data.get(Key);

int cellnum = 0;

for(Object obj : objArr)

{

Cell cell = row.createCell(cellnum++);

if(obj instanceof String)

{

cell.setCellValue((String)obj);

}

else if(obj instanceof Integer)

cell.setCellValue((Integer)obj);

}

}

if(!(St_Id.equals("EXIT")))

{

rownum++;

}

//Auto size my columns that will be filled out with user input info.

for (int i = 0; i < 3; i++)

{

sheet.autoSizeColumn(i);

}

}

try{

//save to excel file

FileOutputStream out = new FileOutputStream(new File("Student Grading Report.xls"));

workbook.write(out);

out.flush();

out.close();

System.out.println("Record added Succesfully...");

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e){

e.printStackTrace();

} catch (Exception e) {

System.out.println(e.getMessage());

}

}

}//pub static void end brace

}//pub class end brace

帮我在此代码,如果任何人都可以..这段代码执行时,它离开输入一个学生数据后,excel中的一行,以及找到学生平均分数的问题。

+0

正确缩进代码将帮助您查看代码中每个点发生了什么 –

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值