導航:首頁 > 編程語言 > 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干什麼用的 瀏覽:357
拽姐是哪個app 瀏覽:858
雲伺服器刪除了還有嗎 瀏覽:232
macbook可以用單片機嘛 瀏覽:307
南陽php招聘 瀏覽:814
去哪裡找按摩師很漂亮的app 瀏覽:818
86x99用簡便演算法計算 瀏覽:830
php截圖flash 瀏覽:273
卸載聯想app哪個好 瀏覽:719
php文字轉圖片 瀏覽:330
豆客後台怎麼加密碼 瀏覽:574
jpg轉換pdf破解版 瀏覽:978
php基礎書籍推薦 瀏覽:777
伺服器與外網不通如何驗證 瀏覽:351
電子版是不是就是文件夾 瀏覽:51
游戲屬性文件加密 瀏覽:464
如何讓安卓手機桌面圖標下移 瀏覽:530
ubuntuphp5環境搭建 瀏覽:101
賭癮解壓視頻 瀏覽:919
晉城移動dns伺服器地址 瀏覽:296