java 写excel 文件的问题,写完后 就把原来的内容清除了
比如我原来(0,0)的位置是“大家好”,现在用JAVA的这个类写入(1,1)的内容是“哈哈”,再打开表格时,只剩下“哈哈”,“大家好“没有了,源代码如下importorg...
比如我原来(0,0)的位置是 “大家好”,现在用JAVA的这个类写入(1,1)的内容是“哈哈”,再打开表格时,只剩下“哈哈”,“大家好“没有了,源代码如下
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class excelRW
{
public static void main(String args[])
{
new excelRW().writeXls("D:/1.xls",1,1);
new excelRW().readXls("D:/1.xls",1,1);
}
//实现对特定单元格的读取
public void readXls(String filepath,int rowNum,int cellNum)
{
try{
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filepath));
HSSFSheet sheet = workbook.getSheet("Sheet1");
HSSFRow row = sheet.getRow(rowNum);
HSSFCell cell = row.getCell(cellNum);
System.out.println("第 "+(rowNum+1)+"行" +" ,第 "+(cellNum+1)+"列的内容是 :" +cell.getStringCellValue());
}
catch(Exception e)
{
System.out.println("已运行 " + e );
}
}
//实现对特定单元格的输入
public void writeXls(String filepath,int rowNum,int cellNum)
{
try{
OutputStream os=new FileOutputStream(filepath);
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sheet1");
HSSFRow row = sheet.createRow(rowNum);
HSSFCell cell = row.createCell(cellNum);
//System.out.println("第 "+(rowNum+1)+"行" +" ,第 "+(cellNum+1)+"列的内容是 :" +cell.getStringCellValue());
cell.setCellValue(new HSSFRichTextString("很高兴!"));
workbook.write(os);
os.close();
}
catch(Exception e)
{
System.out.println("已运行 " + e );
}
}
} 展开
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFCell;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class excelRW
{
public static void main(String args[])
{
new excelRW().writeXls("D:/1.xls",1,1);
new excelRW().readXls("D:/1.xls",1,1);
}
//实现对特定单元格的读取
public void readXls(String filepath,int rowNum,int cellNum)
{
try{
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filepath));
HSSFSheet sheet = workbook.getSheet("Sheet1");
HSSFRow row = sheet.getRow(rowNum);
HSSFCell cell = row.getCell(cellNum);
System.out.println("第 "+(rowNum+1)+"行" +" ,第 "+(cellNum+1)+"列的内容是 :" +cell.getStringCellValue());
}
catch(Exception e)
{
System.out.println("已运行 " + e );
}
}
//实现对特定单元格的输入
public void writeXls(String filepath,int rowNum,int cellNum)
{
try{
OutputStream os=new FileOutputStream(filepath);
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sheet1");
HSSFRow row = sheet.createRow(rowNum);
HSSFCell cell = row.createCell(cellNum);
//System.out.println("第 "+(rowNum+1)+"行" +" ,第 "+(cellNum+1)+"列的内容是 :" +cell.getStringCellValue());
cell.setCellValue(new HSSFRichTextString("很高兴!"));
workbook.write(os);
os.close();
}
catch(Exception e)
{
System.out.println("已运行 " + e );
}
}
} 展开
2个回答
展开全部
你现在的写法是先创建一个新文件叫“D:/1.xls”,如果此文件存在的话,就会覆盖
你得先把原来的文件读进来,然后再写出去
你得先把原来的文件读进来,然后再写出去
追问
具体该怎么实现阿,,
追答
先用 HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filepath));
把已存在内容的 excel读进来,
HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(filepath));
\\处理。。。。。
\\处理后再写出去
OutputStream os=new FileOutputStream(filepath);
workbook.write(os);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询