导航:首页 > 编程语言 > javaexcel追加

javaexcel追加

发布时间:2022-08-28 11:15:59

❶ 利用java(POI)将数据库的内容追加写入EXCEL文件中的第五个sheet,请大神赐教!

HSSFWorkbookwb=newHSSFWorkbook();//创建一个ExcelHSSFSheetsheet=wb.createSheet("sheet1");//创建一个SheetHSSFRowrow=sheet.createRow(0);//创建一个行HSSFCellcell=row.createCell((short)0);//创建一个单元格cell.setEncoding(HSSFCell.ENCODING_UTF_16);cell.setCellValue("序号");//设置值cell=row.createCell((short)1);cell.setEncoding(HSSFCell.ENCODING_UTF_16);cell.setCellValue("姓");这个是最简单的一个例子,下面的参考资料你可以看看.对于新使用者很不错,介绍也很详细,希望能够帮到你

❷ java 如何操作excel 插入一列

1.创建文件。

拟生成一个名为“测试数据.xls”的Excel文件,其中第一个工作表被命名为“第一页”,大致效果如下:
代码(CreateXLS.java):
//生成Excel的类
import java.io.*;
import jxl.*;
import jxl.write.*;
public class CreateXLS
{
public static void main(String args[])
{
try
{
//打开文件
WritableWorkbook book=
Workbook.createWorkbook(new File(“测试.xls”));
//生成名为“第一页”的工作表,参数0表示这是第一页
WritableSheet sheet=book.createSheet(“第一页”,0);
//在Label对象的构造子中指名单元格位置是第一列第一行(0,0)
//以及单元格内容为test
Label label=new Label(0,0,”test”);
//将定义好的单元格添加到工作表中
sheet.addCell(label);
/*生成一个保存数字的单元格
必须使用Number的完整包路径,否则有语法歧义
单元格位置是第二列,第一行,值为789.123*/
jxl.write.Number number = new jxl.write.Number(1,0,789.123);
sheet.addCell(number);
//写入数据并关闭文件
book.write();
book.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
编译执行后,会在当前位置产生一个Excel文件。

2.读取文件
以刚才创建的Excel文件为例,做一个简单的读取操作,程序代码如下:
//读取Excel的类
import java.io.*;
import jxl.*;
public class ReadXLS
{
public static void main(String args[])
{
try
{
Workbook book=
Workbook.getWorkbook(new File(“测试.xls”));
//获得第一个工作表对象
Sheet sheet=book.getSheet(0);
//得到第一列第一行的单元格
Cell cell1=sheet.getCell(0,0);
String result=cell1.getContents();
System.out.println(result);
book.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}
程序执行结果:test

3.修改文件
利用jExcelAPI可以修改已有的Excel文件,修改Excel文件的时候,除了打开文件的方式不同之外,其他操作和创建Excel是一样的。下面的例子是在已经生成的Excel文件中添加一个工作表:
//修改Excel的类,添加一个工作表
import java.io.*;
import jxl.*;
import jxl.write.*;
public class UpdateXLS
{
public static void main(String args[])
{
try
{
//Excel获得文件
Workbook wb=Workbook.getWorkbook(new File(“测试.xls”));
//打开一个文件的副本,并且指定数据写回到原文件
WritableWorkbook book=
Workbook.createWorkbook(new File(“测试.xls”),wb);
//添加一个工作表
WritableSheet sheet=book.createSheet(“第二页”,1);
sheet.addCell(new Label(0,0,”第二页的测试数据”));
book.write();
book.close();
}catch(Exception e)
{
System.out.println(e);
}
}
}

❸ Java-怎么才能用Poi打开已有的excel文件,然后追加一些单元格阿

String filename = "excel名字.xls";
res.setHeader("Content-Type", "application/vnd.ms-excel");
res.setHeader("ContentDisposition", "attachment;filename="
+ new String(filename.getBytes("MS932"), "ISO-8859-1"));
调用一个方法,输出要输出的值,hm是个map
outExcel(hm, res.getOutputStream());
别看下面的方法代码多,其实有用的就几行,主要就是控制row和cell,设置单元格样式,赋值。

private void outExcel(Map hm, ServletOutputStream out) throws IOException, Exception {
String excelPath = excel模板路径;
excelPath = new String(excelPath.getBytes("iso-8859-1"), "utf-8");
POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(excelPath));
HSSFWorkbook wb = new HSSFWorkbook(fs);

HSSFSheet sheet1 = wb.getSheetAt(0);
wb.setSheetName(0, "SheetName");
HSSFPrintSetup ps1 = sheet1.getPrintSetup();
ps1.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE);

HSSFRow row = sheet1.getRow((short) (1));
HSSFCell cell = row.getCell((short) (1));
String dataNumber = (String) hm.get("dataNumber");
cell.setCellValue(new HSSFRichTextString(dataNumber));
String birthAddress = (String) hm.get("birthAddress");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(birthAddress));
String acceptDate = (String) hm.get("acceptDate");
row = sheet1.getRow((short) (2));
cell = row.getCell((short) (1));
cell.setCellValue(new HSSFRichTextString(acceptDate));
String usedistrictCode = (String) hm.get("usedistrictCode");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(usedistrictCode));
String allegationZipCode = (String) hm.get("allegationZipCode");
row = sheet1.getRow((short) (3));
cell = row.getCell((short) (1));
cell.setCellValue(new HSSFRichTextString(allegationZipCode));
String birthName = (String) hm.get("birthName");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(birthName));
String allegationAddress = (String) hm.get("allegationAddress");
row = sheet1.getRow((short) (4));
cell = row.getCell((short) (1));
cell.setCellValue(new HSSFRichTextString(allegationAddress));
String birthTellNumber = (String) hm.get("birthTellNumber");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(birthTellNumber));
String allegationName = (String) hm.get("allegationName");
row = sheet1.getRow((short) (5));
cell = row.getCell((short) (1));
cell.setCellValue(new HSSFRichTextString(allegationName));
// 敪惗尮娆庬僐乕僪
String instrialCode = (String) hm.get("instrialCode");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(instrialCode));
// 怽棫幰背榖斣崋
String allegationTellNumber = (String) hm.get("allegationTellNumber");
row = sheet1.getRow((short) (6));
cell = row.getCell((short) (1));
cell.setCellValue(new HSSFRichTextString(allegationTellNumber));
// 敪惗尮巤愝摍
String birthFacilitiesetc = (String) hm.get("birthFacilitiesetc");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(birthFacilitiesetc));
// 怽棫撪梕
String allegation = (String) hm.get("allegation");
row = sheet1.getRow((short) (7));
cell = row.getCell((short) (1));
cell.setCellValue(new HSSFRichTextString(allegation));
// 挷嵏巜摫撪梕
String guidance = (String) hm.get("guidance");
row = sheet1.getRow((short) (8));
cell = row.getCell((short) (1));
cell.setCellValue(new HSSFRichTextString(guidance));
// 应掕擭宁担1
String sokuDate1 = (String) hm.get("sokuDate1");
row = sheet1.getRow((short) (9));
cell = row.getCell((short) (2));
cell.setCellValue(new HSSFRichTextString(sokuDate1));
// 应掕擭宁担2
String sokuDate2 = (String) hm.get("sokuDate2");
cell = row.getCell((short) (3));
cell.setCellValue(new HSSFRichTextString(sokuDate2));
// 应掕擭宁担3
String sokuDate3 = (String) hm.get("sokuDate3");
cell = row.getCell((short) (4));
cell.setCellValue(new HSSFRichTextString(sokuDate3));
// 应掕擭宁担4
String sokuDate4 = (String) hm.get("sokuDate4");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(sokuDate4));
// 应掕擭宁担5
String sokuDate5 = (String) hm.get("sokuDate5");
cell = row.getCell((short) (6));
cell.setCellValue(new HSSFRichTextString(sokuDate5));
// 应掕擭宁担6
String sokuDate6 = (String) hm.get("sokuDate6");
cell = row.getCell((short) (7));
cell.setCellValue(new HSSFRichTextString(sokuDate6));
// 应掕帪岗1
String sokuTime1 = (String) hm.get("sokuTime1");
row = sheet1.getRow((short) (10));
cell = row.getCell((short) (2));
cell.setCellValue(new HSSFRichTextString(sokuTime1));
// 应掕帪岗2
String sokuTime2 = (String) hm.get("sokuTime2");
cell = row.getCell((short) (3));
cell.setCellValue(new HSSFRichTextString(sokuTime2));
// 应掕帪岗3
String sokuTime3 = (String) hm.get("sokuTime3");
cell = row.getCell((short) (4));
cell.setCellValue(new HSSFRichTextString(sokuTime3));
// 应掕帪岗4
String sokuTime4 = (String) hm.get("sokuTime4");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(sokuTime4));
// 应掕帪岗5
String sokuTime5 = (String) hm.get("sokuTime5");
cell = row.getCell((short) (6));
cell.setCellValue(new HSSFRichTextString(sokuTime5));
// 应掕帪岗6
String sokuTime6 = (String) hm.get("sokuTime6");
cell = row.getCell((short) (7));
cell.setCellValue(new HSSFRichTextString(sokuTime6));
// 帋桠斣崋1
String shiryoNumber1 = (String) hm.get("shiryoNumber1");
row = sheet1.getRow((short) (11));
cell = row.getCell((short) (2));
cell.setCellValue(new HSSFRichTextString(shiryoNumber1));
// 帋桠斣崋2
String shiryoNumber2 = (String) hm.get("shiryoNumber2");
cell = row.getCell((short) (3));
cell.setCellValue(new HSSFRichTextString(shiryoNumber2));
// 帋桠斣崋3
String shiryoNumber3 = (String) hm.get("shiryoNumber3");
cell = row.getCell((short) (4));
cell.setCellValue(new HSSFRichTextString(shiryoNumber3));
// 帋桠斣崋4
String shiryoNumber4 = (String) hm.get("shiryoNumber4");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(shiryoNumber4));
// 帋桠斣崋5
String shiryoNumber5 = (String) hm.get("shiryoNumber5");
cell = row.getCell((short) (6));
cell.setCellValue(new HSSFRichTextString(shiryoNumber5));
// 帋桠斣崋6
String shiryoNumber6 = (String) hm.get("shiryoNumber6");
cell = row.getCell((short) (7));
cell.setCellValue(new HSSFRichTextString(shiryoNumber6));
// 婥壏1
String temp1 = (String) hm.get("temp1");
row = sheet1.getRow((short) (12));
cell = row.getCell((short) (2));
cell.setCellValue(new HSSFRichTextString(temp1));
// 婥壏2
String temp2 = (String) hm.get("temp2");
cell = row.getCell((short) (3));
cell.setCellValue(new HSSFRichTextString(temp2));
// 婥壏3
String temp3 = (String) hm.get("temp3");
cell = row.getCell((short) (4));
cell.setCellValue(new HSSFRichTextString(temp3));
// 婥壏4
String temp4 = (String) hm.get("temp4");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(temp4));
// 婥壏5
String temp5 = (String) hm.get("temp5");
cell = row.getCell((short) (6));
cell.setCellValue(new HSSFRichTextString(temp5));
// 婥壏6
String temp6 = (String) hm.get("temp6");
cell = row.getCell((short) (7));
cell.setCellValue(new HSSFRichTextString(temp6));
// 幖搙1
String humid1 = (String) hm.get("humid1");
row = sheet1.getRow((short) (13));
cell = row.getCell((short) (2));
cell.setCellValue(new HSSFRichTextString(humid1));
// 幖搙2
String humid2 = (String) hm.get("humid2");
cell = row.getCell((short) (3));
cell.setCellValue(new HSSFRichTextString(humid2));
// 幖搙3
String humid3 = (String) hm.get("humid3");
cell = row.getCell((short) (4));
cell.setCellValue(new HSSFRichTextString(humid3));
// 幖搙4
String humid4 = (String) hm.get("humid4");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(humid4));
// 幖搙5
String humid5 = (String) hm.get("humid5");
cell = row.getCell((short) (6));
cell.setCellValue(new HSSFRichTextString(humid5));
// 幖搙6
String humid6 = (String) hm.get("humid6");
cell = row.getCell((short) (7));
cell.setCellValue(new HSSFRichTextString(humid6));
// 晽岦1
String windCode1 = (String) hm.get("windCode1");
row = sheet1.getRow((short) (14));
cell = row.getCell((short) (2));
cell.setCellValue(new HSSFRichTextString(windCode1));
// 晽岦2
String windCode2 = (String) hm.get("windCode2");
cell = row.getCell((short) (3));
cell.setCellValue(new HSSFRichTextString(windCode2));
String windCode3 = (String) hm.get("windCode3");
cell = row.getCell((short) (4));
cell.setCellValue(new HSSFRichTextString(windCode3));
String windCode4 = (String) hm.get("windCode4");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(windCode4));
String windCode5 = (String) hm.get("windCode5");
cell = row.getCell((short) (6));
cell.setCellValue(new HSSFRichTextString(windCode5));
String windCode6 = (String) hm.get("windCode6");
cell = row.getCell((short) (7));
cell.setCellValue(new HSSFRichTextString(windCode6));
String windSpeed1 = (String) hm.get("windSpeed1");
row = sheet1.getRow((short) (15));
cell = row.getCell((short) (2));
cell.setCellValue(new HSSFRichTextString(windSpeed1));
String windSpeed2 = (String) hm.get("windSpeed2");
cell = row.getCell((short) (3));
cell.setCellValue(new HSSFRichTextString(windSpeed2));
String windSpeed3 = (String) hm.get("windSpeed3");
cell = row.getCell((short) (4));
cell.setCellValue(new HSSFRichTextString(windSpeed3));
String windSpeed4 = (String) hm.get("windSpeed4");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(windSpeed4));
String windSpeed5 = (String) hm.get("windSpeed5");
cell = row.getCell((short) (6));
cell.setCellValue(new HSSFRichTextString(windSpeed5));
String windSpeed6 = (String) hm.get("windSpeed6");
cell = row.getCell((short) (7));
cell.setCellValue(new HSSFRichTextString(windSpeed6));
String pick1 = (String) hm.get("pick1");
row = sheet1.getRow((short) (16));
cell = row.getCell((short) (2));
cell.setCellValue(new HSSFRichTextString(pick1));
String pick2 = (String) hm.get("pick2");
cell = row.getCell((short) (3));
cell.setCellValue(new HSSFRichTextString(pick2));
String pick3 = (String) hm.get("pick3");
cell = row.getCell((short) (4));
cell.setCellValue(new HSSFRichTextString(pick3));
String pick4 = (String) hm.get("pick4");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(pick4));
String pick5 = (String) hm.get("pick5");
cell = row.getCell((short) (6));
cell.setCellValue(new HSSFRichTextString(pick5));
String pick6 = (String) hm.get("pick6");
cell = row.getCell((short) (7));
cell.setCellValue(new HSSFRichTextString(pick6));
String smell1 = (String) hm.get("smell1");
row = sheet1.getRow((short) (17));
cell = row.getCell((short) (2));
cell.setCellValue(new HSSFRichTextString(smell1));
String smell2 = (String) hm.get("smell2");
cell = row.getCell((short) (3));
cell.setCellValue(new HSSFRichTextString(smell2));
String smell3 = (String) hm.get("smell3");
cell = row.getCell((short) (4));
cell.setCellValue(new HSSFRichTextString(smell3));
String smell4 = (String) hm.get("smell4");
cell = row.getCell((short) (5));
cell.setCellValue(new HSSFRichTextString(smell4));
String smell5 = (String) hm.get("smell5");
cell = row.getCell((short) (6));
cell.setCellValue(new HSSFRichTextString(smell5));
String smell6 = (String) hm.get("smell6");
cell = row.getCell((short) (7));
cell.setCellValue(new HSSFRichTextString(smell6));
wb.write(out);
out.close();
}

❹ java 在Jframe里用jxl怎么把数据追加进excel里,不要覆盖原有数据

blic static void main(String[] args) throws BiffException,IOException,WriteException {
try {
InputStream is = new FileInputStream("F:/EXCEL/ZY_T_D_DRIVERINCOME.xls");
jxl.Workbook rwb = Workbook.getWorkbook(is);
System.out.println(rwb);
Sheet[] rsArray = rwb.getSheets();
Sheet rs = rsArray[0];

Cell c;
for(int i=0;i<rs.getRows();i++){
for(int j=0;j<rs.getColumns();j++){
c=rs.getCell(j,i);
String strc = c.getContents();

System.out.print(strc);

rwb.close();
}

}

} catch (Exception e) {
e.printStackTrace();
}
}
将取到的数据可以先组装成List类型数据,然后再写,或者直接写入数据库,数据库应该有对应的字段

❺ java中怎样在已存在数据的excel中追加数据,不能覆盖前面的数据。

先读出excel中内容的总行数啊
然后从指定行开始插入数据,或者干脆数据都读到一个数组里,然后再把新数据追加到数组中,覆写文件

❻ JAVA如何向指定的EXCEL单元格中写入数据

我们项目里用的 供你参考,没写全,你可以自己网络下

import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableImage;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

/**
* 导出excel
* @param reportParams 导出excel列名标示
* @param list导出excel值
* @param headersexcel头
* @param reportName·excel的sheet名
* @param response
* @param startRow从哪一行开始放list值
* @param startCol 从哪一列开始放list值
* @return
*/
public boolean report(ReportBean rb, List list, XlsHeaderBean[] headers, String reportName, HttpServletResponse response, int startRow, int startCol, HttpServletRequest request){
WritableWorkbook wwb = null;
OutputStream os;
boolean flag = true;
Date date = new Date();
DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String strDate = format.format(date);
try {
// 用Workbook类的工厂方法创建工作薄(Workbook)对象
response.setContentType("application/x-msdownload");
String sheetName = "report";
sheetName = sheetName.replaceAll(":", "").replaceAll("[)]", "")
.replaceAll("[(]", "");
// 这里解释一下
// attachment; 这个代表要下载的,如果去掉就编程直接打开了
// filename是文件名,另存为或者下载时,为默认的文件名
response.addHeader("Content-Disposition", "attachment; filename="
+ new String(sheetName.getBytes("UTF-8"), "ISO-8859-1")+ strDate
+ ".xls");
os = response.getOutputStream();
wwb = Workbook.createWorkbook(os);
} catch (IOException e) {
e.printStackTrace();
return flag = false;
}
if (wwb != null) {
// 创建一个可写入的工作表
// Workbook的createSheet方法两个参数,1名称,2位置
WritableSheet ws = wwb.createSheet(reportName, 0);
// 下面开始添加单元格
// 导出excel
try {
Label labelC = null;
for(int j = 0; j < headers.length; j++){
if(headers[j].isUnion()){
ws.mergeCells(headers[j].getCol(), headers[j].getRow(), headers[j].getCol()+headers[j].getColLength(), headers[j].getRow()+headers[j].getRowLength());
}
labelC = new Label(headers[j].getCol(), headers[j].getRow(), headers[j].getValue());
ws.addCell(labelC);
}
} catch (RowsExceededException e) {
e.printStackTrace();
} catch (WriteException e) {
e.printStackTrace();
}
String value = null;
for (int i = 0; i < list.size(); i++) {
Map values = (Map)list.get(i);
for (int j = 0; j < rb.getReportParams().length; j++) {
// 这里需要注意的是,在Excel中,第一个参数表示列,第二个表示行
Label labelC;
if(values.get(rb.getReportParams()[j]) == null){
value = "";
} else {
if(values.get(rb.getReportParams()[j]) instanceof java.util.Date){
value = format.format(values.get(rb.getReportParams()[j]));
} else if(values.get(rb.getReportParams()[j]) instanceof java.math.BigDecimal){
value = values.get(rb.getReportParams()[j]).toString();
} else {
value = values.get(rb.getReportParams()[j]).toString();
}
}
labelC = new Label(j+startCol, i + startRow, value);
try {
// 将生成的单元格添加到工作表中
ws.addCell(labelC);
} catch (RowsExceededException e) {
e.printStackTrace();
return flag = false;
} catch (WriteException e) {
e.printStackTrace();
return flag = false;
}
}
}
if(rb.isHasImg()){
String rootPath = this.getServletContext().getRealPath("savefiles");
String imgPath = rb.getImgPath();
if(rootPath != null && rootPath.compareTo("") != 0 && imgPath != null && imgPath.compareTo("") !=0){
String[] strs = imgPath.split("\\/");
String imgName = strs[strs.length-1];
File file = new File(rootPath + File.separator + imgName);
if(file.exists()){
WritableImage wi = new WritableImage(0, startRow + list.size() + 2,12,20, file);
ws.addImage(wi);
}
}
}
try {
// 从内存中写入文件中
wwb.write();
wwb.close();
return flag;
} catch (IOException e) {
e.printStackTrace();
return flag = false;
} catch (WriteException e) {
e.printStackTrace();
return flag = false;
}
}
return flag;
}

❼ 使用java如何在excel已有值的单元格中继续添加值

你把原本的值拿出来,拼到一起然后再存回去呀

至于 alt+enter ,你可以先放到一个单元格里,然后用程序读出来看看是啥样的

阅读全文

与javaexcel追加相关的资料

热点内容
卸载联想app哪个好 浏览:716
php文字转图片 浏览:326
豆客后台怎么加密码 浏览:572
jpg转换pdf破解版 浏览:976
php基础书籍推荐 浏览:773
服务器与外网不通如何验证 浏览:349
电子版是不是就是文件夹 浏览:48
游戏属性文件加密 浏览:460
如何让安卓手机桌面图标下移 浏览:526
ubuntuphp5环境搭建 浏览:99
赌瘾解压视频 浏览:917
晋城移动dns服务器地址 浏览:294
php开源文库系统 浏览:134
android记事本源码 浏览:405
安卓11小游戏怎么玩法 浏览:188
gif有损压缩 浏览:935
windows下安装linux命令操作 浏览:842
米家app怎么设置进门亮灯 浏览:652
任我行服务器为什么会影响截图 浏览:296
安卓留言板怎么删除 浏览:18