java 如何将字符串写入一个已有的excel表格中
比如有一组字符串result(这是一组处理结果比如inti=0;i<5;i++然后输出5个result)。excel的位置是D:\\111\\R1.xls然后我想依次放到...
比如有一组字符串result(这是一组处理结果 比如int i=0;i<5;i++ 然后输出5个result )。excel的位置是D:\\111\\R1.xls 然后我想依次放到 表格的第二列(12345行) 这样 不知道代码应该怎么实现?
展开
3个回答
展开全部
import java.io.File;
import java.io.IOException;
import jxl.Workbook;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class SaveExcel {
public void writeExcel(String path) {
WritableWorkbook wb;
try {
wb = Workbook.createWorkbook(new File(path));
WritableSheet ws = wb.createSheet("sheet1", 0);
for(int i=0;i<5;i++){
Number labelN = new Number(1, i, i);
ws.addCell(labelN);
}
wb.write();
wb.close();
} catch (IOException e) {
e.printStackTrace();
}
catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new SaveExcel().writeExcel("D:\\111\\R1.xls");
}
}
你要去下一个jxl.jar
import java.io.IOException;
import jxl.Workbook;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class SaveExcel {
public void writeExcel(String path) {
WritableWorkbook wb;
try {
wb = Workbook.createWorkbook(new File(path));
WritableSheet ws = wb.createSheet("sheet1", 0);
for(int i=0;i<5;i++){
Number labelN = new Number(1, i, i);
ws.addCell(labelN);
}
wb.write();
wb.close();
} catch (IOException e) {
e.printStackTrace();
}
catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new SaveExcel().writeExcel("D:\\111\\R1.xls");
}
}
你要去下一个jxl.jar
追问
Number labelN = new Number(1, i, i);这个Number()的三个参数分别是什么意思?
追答
第一个是第几列,列数从0开始,1,表示第二列
第二个是第几行,行数从0开始。
第三个是想显示的东西
来自:求助得到的回答
展开全部
操作Excel,用POI这个类库比较多。 下面是示例代码
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import java.io.*;
public class Sample3_1{
public static void main(String[] args){
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();
Row row1 = sheet.createRow(1);
Row row2 = sheet.createRow(2);
Cell cell1_0 = row1.createCell(0);
Cell cell1_1 = row1.createCell(1);
Cell cell1_2 = row1.createCell(2);
Cell cell2_0 = row2.createCell(0);
Cell cell2_1 = row2.createCell(1);
Cell cell2_2 = row2.createCell(2);
cell1_0.setCellValue(10);
cell1_1.setCellValue(-8.5);
cell1_2.setCellValue(3.14);
cell2_0.setCellValue("Hello");
cell2_1.setCellValue("表形式");
cell2_2.setCellValue("3.14");
FileOutputStream out = null;
try{
out = new FileOutputStream("sample3_1.xls");
wb.write(out);
}catch(IOException e){
System.out.println(e.toString());
}finally{
try {
out.close();
}catch(IOException e){
System.out.println(e.toString());
}
}
}
}
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import java.io.*;
public class Sample3_1{
public static void main(String[] args){
Workbook wb = new HSSFWorkbook();
Sheet sheet = wb.createSheet();
Row row1 = sheet.createRow(1);
Row row2 = sheet.createRow(2);
Cell cell1_0 = row1.createCell(0);
Cell cell1_1 = row1.createCell(1);
Cell cell1_2 = row1.createCell(2);
Cell cell2_0 = row2.createCell(0);
Cell cell2_1 = row2.createCell(1);
Cell cell2_2 = row2.createCell(2);
cell1_0.setCellValue(10);
cell1_1.setCellValue(-8.5);
cell1_2.setCellValue(3.14);
cell2_0.setCellValue("Hello");
cell2_1.setCellValue("表形式");
cell2_2.setCellValue("3.14");
FileOutputStream out = null;
try{
out = new FileOutputStream("sample3_1.xls");
wb.write(out);
}catch(IOException e){
System.out.println(e.toString());
}finally{
try {
out.close();
}catch(IOException e){
System.out.println(e.toString());
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
import java.io.File;
import java.io.IOException;
import jxl.Workbook;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class SaveExcel {
public void writeExcel(String path) {
WritableWorkbook wb;
try {
wb = Workbook.createWorkbook(new File(path));
WritableSheet ws = wb.createSheet("sheet1", 0);
for(int i=0;i<5;i++){
Number labelN = new Number(1, i, i);
ws.addCell(labelN);
}
wb.write();
wb.close();
} catch (IOException e) {
e.printStackTrace();
}
catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new SaveExcel().writeExcel("D:\\111\\R1.xls");
}
}
import java.io.IOException;
import jxl.Workbook;
import jxl.write.Number;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class SaveExcel {
public void writeExcel(String path) {
WritableWorkbook wb;
try {
wb = Workbook.createWorkbook(new File(path));
WritableSheet ws = wb.createSheet("sheet1", 0);
for(int i=0;i<5;i++){
Number labelN = new Number(1, i, i);
ws.addCell(labelN);
}
wb.write();
wb.close();
} catch (IOException e) {
e.printStackTrace();
}
catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void main(String[] args) {
new SaveExcel().writeExcel("D:\\111\\R1.xls");
}
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询