我写了一个用于在Excel工作表中写入数据的代码。在这里我必须在多个单元格中写入数据。但是它显示了一些错误。对于一个单元格,它能够更改数据。我保持 for loop 用于更改多个单元格中的数据。为此,它显示错误。 任何人都可以告诉我,我错了。
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import java.lang.String;
import javax.swing.JOptionPane;
import jxl.Cell;
import org.apache.poi.hssf.util.CellReference;
import org.apache.poi.ss.formula.functions.Column;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFComment;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class Sele1
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
String FileName = "C:\\Users\\u304081\\Desktop\\Java\\new.xlsx";
try
{
FileInputStream fileInputStream3 = new FileInputStream(FileName);
File outputsheetfile1 = new File(FileName);
if(outputsheetfile1.exists())
{
System.out.println("File existed");
try
{
XSSFWorkbook ObjWorkBook = new XSSFWorkbook(fileInputStream3);
XSSFSheet DriverTableSheet = ObjWorkBook.getSheetAt(0);
for(int i=1;i<3;i++)
{
XSSFRow row1 = DriverTableSheet.getRow(i);
XSSFCell Cell1 = row1.getCell(0);
System.out.println("Cell1"+ Cell1);
//System.out.println("Cell2"+ Cell2);
String str = "Abc";
Cell1.setCellValue(str);
FileOutputStream out1 = new FileOutputStream (FileName,false);
ObjWorkBook.write(out1);
fileInputStream3.close();
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我得到的错误是:
ObjWorkBook.write(out1);
`"poi-bin-3.9-20121203\poi-3.9\poi-ooxml-3.9-20121203.jar has no source attachment"`
答案 0 :(得分:1)
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Course Pack Resolution Details");
outputFileName = outPut.getAbsolutePath();
int rownum = 0;`enter code here`
for (int i = 0; i < dataList.size(); i++) {
Object[] objArr = dataList.get(i);
HSSFRow row = sheet.createRow(rownum++);
int cellnum = 0;
for (Object obj : objArr) {
Cell cell = row.createCell(cellnum++);
sheet.autoSizeColumn((short) cellnum);
if (obj instanceof Date) {
cell.setCellValue((Date) obj);
} else if (obj instanceof Boolean) {
cell.setCellValue((Boolean) obj);
} else if (obj instanceof String) {
cell.setCellValue((String) obj);
} else if (obj instanceof Double) {
cell.setCellValue((Double) obj);
}
}
}
if (outPut.exists()) {
outPut.delete();
}
FileOutputStream out =
new FileOutputStream(outPut);
workbook.write(out);
DataList是Array Object的ArrayList,因此您可以根据需要输入尽可能多的数据。
DataList示例:
dataList.add(new Object[]{"Sr No.", "Cols1", "cols2", "cols3"......."colsn"});
您可以在列表中插入的相应数据。这个例子是.xls格式,如果你想要.xlsx然后使用xssfworkbook。
可以帮到你。
答案 1 :(得分:0)
您提到的错误:
ObjWorkBook.write(out1); Here it is showing Error as "poi-bin-3.9-20121203\poi-3.9\poi-ooxml-3.9-20121203.jar has no source attachment"
似乎与您提到的有关在多个单元格中编写数据以提高数据的问题无关。
你可能想看看这个:
答案 2 :(得分:0)
=======在播放框架中的excel文件中写入数据===============
public class Application extends Controller {
/*
* public Result index() { return
* ok(index.render("Your new application is ready.")); }
*/
public Result readExcel() throws FileNotFoundException {
try{
InputStream ExcelFileToRead = new FileInputStream("/home/jagasan/workspace-play/excelApp/public/ExcelFile3.xlsx");
HSSFWorkbook wb = new HSSFWorkbook(ExcelFileToRead);
HSSFSheet sheet=wb.getSheetAt(0);
HSSFRow row;
HSSFCell cell;
Iterator rows = sheet.rowIterator();
boolean flag=false;
while (rows.hasNext())
{
row=(HSSFRow) rows.next();
if(flag==false)
{
flag=true;
continue;
}
Iterator cells = row.cellIterator();
ExcelFile excelfile=new ExcelFile();
int i=0;
while (cells.hasNext())
{
cell=(HSSFCell) cells.next();
if(i==0)
excelfile.setEmpNo((int)cell.getNumericCellValue());
if(i==1)
excelfile.setName(cell.getStringCellValue());
if(i==2)
excelfile.setSalary(cell.getNumericCellValue());
i++;
}
excelfile.save();
}
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("error");
}
return ok("successful");
}
=======从excel读取数据并在playframework中存储在数据库中=======
libraryDependencies ++= Seq(
javaJdbc,
cache,
javaWs,
"org.apache.poi" % "poi" % "3.8", "org.apache.poi" % "poi-ooxml" % "3.9"
)
======在build.sbt =======
使用依赖jar文件表示apache API
{{1}}