用Java轻松实现数据库数据导出为Excel表格 (java导出数据库数据为excel)

2023-07-27 17:22:42 数据 数据库 导出

在数据处理过程中,将数据库中的数据导出为Excel表格是一项非常常见的操作。利用Java可以快速、高效地将数据导出为Excel表格,大大提高了数据处理效率和准确性。

一、Java实现Excel导出的准备工作

在使用Java实现Excel导出之前,需要进行一些准备工作,包括:

1、引入POI和Excel工具包

POI和Excel工具包是用于实现Java导出Excel的重要工具,需要先引入这两个工具包。

2、连接数据库

要导出数据库的数据,需要先连接数据库,获取需要导出的数据。

3、创建Excel文件

在导出数据之前,需要先创建一个Excel文件,将需要导出的数据写入到这个文件中。

二、将数据导出为Excel表格的具体实现

1、创建Excel文件

我们需要创建一个Excel文件,以便把数据写入到其中。创建Excel文件的步骤如下:

“`

//创建工作簿

Workbook workbook=new HSSFWorkbook();

//创建工作表

Sheet sheet =workbook.createSheet(“sheet1”);

“`

这里我们创建了一个工作簿和一个工作表。其中工作簿是Excel的更高级别的中间对象,而工作表则是工作簿下的具体表格。

2、获取需要导出的数据,并将其写入到Excel文件中

在准备工作完成之后,我们需要从数据库中获取需要导出的数据,并将其写入到创建好的Excel文件中。具体的实现方法如下:

“`

//获取连接

Connection con=this.getConnection();

Statement st = null;

ResultSet rs = null;

try {

    st = con.createStatement();

    rs = st.executeQuery(“select * from student”);//查询的SQL语句

    int rowNum = 1;

    // 添加excel的表头

Row headRow = sheet.createRow(0);

ResultSetMetaData rd = rs.getMetaData();

for (int i = 1; i

String columnName = rd.getColumnName(i);

Cell headCell = headRow.createCell(i-1);

headCell.setCellValue(columnName);

}

    while(rs.next()){

        Row row = sheet.createRow(rowNum);

        for(int i=0;i

            Cell cell = row.createCell(i);

            Object objVal = rs.getObject(i+1);

            if(objVal!=null){

                cell.setCellValue(objVal.toString());

            }

        }

        rowNum++;

    }

    

}catch(Exception e){

    e.printStackTrace();

} finally {

    try{

        if(st!=null) st.close();

        if(rs!=null) rs.close();

    }catch(SQLException e){

        e.printStackTrace();

    }

}

“`

在写入数据的过程中,我们需要注意如下问题:

① 在写入数据之前,我们需要先添加Excel表格的表头,以便于查看数据的内容;

② 在从ResultSet对象中获取每列对应的列名、列类型和列值时,一定要使用ResultSetMetaData类的方法来获取,这样可以更好的获取元数据信息,更加准确地组织数据。

3、保存Excel文件

数据写入Excel文件结束后,我们需要将这个文件保存到本地,可以使用FileOutputStream实现文件的输出和保存。具体的实现方法如下:

“`

File file = new File(“D:/student.xlsx”);

FileOutputStream fos = null;

try {

    if(!file.exists()){

        file.createNewFile();

    }

    fos=new FileOutputStream(file);

workbook.write(fos);

} catch (Exception e) {

    e.printStackTrace();

}finally {

    try {

        if (fos != null) {

            fos.close();

        }

    } catch (IOException e) {

        e.printStackTrace();

    }

}

“`

在保存Excel文件的过程中,我们需要注意如下问题:

① 在保存Excel文件之前,需要先判断文件是否存在,如果不存在,则需要创建新的文件;

② 在保存Excel文件之前,需要使用FileOutputStream来创建文件输出流,将文件的输出流与Excel文件进行关联,最后调用workbook.write(fos)方法来写入数据。

三、

利用Java实现数据库数据导出为Excel表格是一项非常实用的技术,可以大大提高数据处理的效率和准确性。Java中提供了POI和Excel工具包可以用来快速导出Excel文件,并且通过连接数据库和使用ResultSet对象可以快速获取需要导出的数据,然后将数据写入到Excel文件中并保存到本地。

相关问题拓展阅读:

  • java导出excel
  • 如何从java输出到excel
  • Java怎样将数据库中数据导出为Excel文件,求完整例子附带数据库。

java导出excel

试试double型

java导出Excel

java 代码 /* * Generated by MyEclipse Struts * Template path: templates/java/JavaClass.vtl */ package com.axon.fable.sams.view.action; import java.io.IOException; import java.io.OutputStream; import java.util.List; import javax.serv …

java导出Excel例举方式

方法一:导出Excel数据的插件jexcelapi

程序实例如下:

public void exportClassroom(OutputStream os) throws PaikeException {

try {

WritableWorkbook wbook = Workbook.createWorkbook(os); //建立excel文件

WritableSheet wsheet = wbook.createSheet(“教室信息表”, 0); //工作表名称

//设置Excel字体

WritableFont wfont = new WritableFont(WritableFont.ARIAL, 16,

WritableFont.BOLD, false,

jxl.format.UnderlineStyle.NO_UNDERLINE,

jxl.format.Colour.BLACK);

WritableCellFormat titleFormat = new WritableCellFormat(wfont);

String title = { “教室名”, “容 量”, “类 型”, “其他说明”兆源 };

//设置锋猜渗银脊Excel表头

for (int i = 0; i </p> <p>Label excelTitle = new Label(i, 0, title<i>, titleFormat); </p> <p><p>wsheet.addCell(excelTitle); </p> <p>} </p> <p>int c = 1; //用于循环时Excel的行号 </p> <p>ClassroomService cs = new ClassroomService(); </p> <p>List list = cs.findAllClassroom(); //这个是从数据库中取得要导出的数据 </p> <p>Iterator it = list.iterator(); </p> <p>while (it.hasNext()) { </p> <p>ClassroomDTO crdto = (ClassroomDTO) it.next(); </p> <p>Label content1 = new Label(0, c, crdto.getRoomname()); </p> <p>Label content2 = new Label(1, c, crdto.getCapicity().toString()); </p> <p>Label content3 = new Label(2, c, crdto.getRoomTypeId() </p> <p>.toString()); </p> <p>Label content4 = new Label(3, c, crdto.getRemark()); </p> <p>wsheet.addCell(content1); </p> <p>wsheet.addCell(content2); </p> <p>wsheet.addCell(content3); </p> <p>wsheet.addCell(content4); </p> <p>c++; </p> <p>} </p> <p>wbook.write(); //写入文件 </p> <p>wbook.close(); </p> <p>os.close(); </p> <p>} catch (Exception e) { </p> <p>throw new PaikeException(“导出文件出错”); </p> <p>} </p> <p>} </p> <p>方法二:直接用Java代码实现导出Excel报表 </p> <p>/* </p> <p>* Generated by MyEclipse Struts </p> <p>* Template path: templates/java/JavaClass.vtl </p> <p>*/ </p> <p>package com.axon.fable.sams.view.action; </p> <p>import java.io.IOException; </p> <p>import java.io.OutputStream; </p> <p>import java.util.List; </p> <p>import javax.servlet.http.HttpServletRequest; </p> <p>import javax.servlet.http.HttpServletResponse; </p> <p>import jxl.Workbook; </p> <p>import jxl.write.WriteException; </p> <p>import jxl.write.biff.RowsExceededException; </p> <p>import org.apache.struts.action.ActionForm; </p> <p>import org.apache.struts.action.ActionForward; </p> <p>import org.apache.struts.action.ActionMapping; </p> <p>import org.hibernate.HibernateException; </p> <p>import org.hibernate.Query; </p> <p>import org.hibernate.Session; </p> <p>import org.hibernate.Transaction; </p> <p>import com.axon.fable.empolderpackage.out.OutJavaScript; </p> <p>import com.axon.fable.empolderpackage.page.Pager; </p> <p>import com.axon.fable.empolderpackage.string.MyPublic; </p> <p>import com.axon.fable.sams.common.BaseAction; </p> <p>import com.axon.fable.sams.exception.AppBusinessException; </p> <p>import com.axon.fable.sams.exception.AppSystemException; </p> <p>/** </p> <p>* MyEclipse Struts </p> <p>* Creation date:</p> <p>* </p> <p>* XDoclet definition: </p> <p>* @struts.action path=”/axon” name=”axonForm” input=”/samspage/zm/axon.jsp” parameter=”method” scope=”request” validate=”true” </p> <p>* @struts.action-forward name=”success” path=”/samspage/zm/content.jsp” </p> <p>*/ </p> <p>public class StshipoperationAction extends BaseAction { </p> <p>/* </p> <p>* Generated Methods </p> <p>*/ </p> <p>private static Session session=null; </p> <p>private static Transaction ts=null; </p> <p>private static Query queryC=null; </p> <p>private static Query queryR=null; </p> <p>private static Query query=null; </p> <p>private static List list=null; </p> <p>private static Integer startRow; </p> <p>private static Integer ncurrentPage; </p> <p>private static Integer cell; </p> <p>private static String property; </p> <p>private static String sql; </p> <p>private static String type; </p> <p>private static String condition ;//是否导出当前页 </p> <p>private static String currentPage; </p> <p>private static String from ; </p> <p>private static String pactdata; </p> <p>private static String voyagename; </p> <p>private static String voyageno; </p> <p>private static String dwt ; </p> <p>private static String hirefrom ; </p> <p>private static String deliveryposion ; </p> <p>private static String redeliveryposion ; </p> <p>private static String sheepowner ; </p> <p>private static String addr; </p> <p>private static String addcomm; </p> <p>private static String rent; </p> <p>private static String fileName ; </p> <p>private static OutputStream os; </p> <p>@Override </p> <p>public ActionForward findAll(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { </p> <p>// TODO Auto-generated method stub </p> <p>return null; </p> <p>} </p> <p>@Override </p> <p>public ActionForward findById(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { </p> <p>// TODO Auto-generated method stub </p> <p>return null; </p> <p>} </p> <p>@Override </p> <p>public ActionForward save(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { </p> <p>// TODO Auto-generated method stub </p> <p>return null; </p> <p>} </p> <p>public static String strNull(Object nullStr,String newStr,Integer cell){ </p> <p>if(nullStr==null||nullStr.equals(“”)){return newStr;}else{cell+=1;return nullStr+””;} </p> <p>} </p> <p>public static String getStr(String str,Integer cell){ </p> <p>if(str==null||str.trim().equals(“”)){return “”;}else{cell+=1;return “,”+str;} </p> <p>} </p> <p>public static String getExcelTile(String title){ </p> <p>if(title==null) </p> <p>return “”; </p> <p>if(title.equals(“modela.stsid”)) </p> <p>return “编号”; </p> <p>if(title.equals(“modelc.pactdata”)) </p> <p>return “合同日期”; </p> <p>if(title.equals(“modela.voyagename”)) </p> <p>return “航名”; </p> <p>if(title.equals(“modela.voyageno”)) </p> <p>return “航次”; </p> <p>if(title.equals(“modelc.dwt”)) </p> <p>return “DWT”; </p> <p>if(title.equals(“modelc.hirefrom”)) </p> <p>return “受载期”; </p> <p>if(title.equals(“modela.deliveryposion”)) </p> <p>return “交船地点”; </p> <p>if(title.equals(“modela.redeliveryposion”)) </p> <p>return “还船地点”; </p> <p>if(title.equals(“modelc.sheepowner”)) </p> <p>return “联系人”; </p> <p>if(title.equals(“modelc.addr”)) </p> <p>return “经纪人拥金”; </p> <p>if(title.equals(“modelc.addcomm”)) </p> <p>return “ADD COMM”; </p> <p>if(title.equals(“modelc.rent”)) </p> <p>return “租金”; </p> <p>return “”; </p> <p>} </p> <p>public ActionForward exporVoyagesInfoToExcel(ActionMapping mapping, ActionForm form, </p> <p>HttpServletRequest request, HttpServletResponse response) { </p> <p>list=null; </p> <p>startRow=0; </p> <p>ncurrentPage=1; </p> <p>cell=0; </p> <p>type =request.getParameter(“type”); </p> <p>condition =request.getParameter(“condition”);//是否导出当前页 </p> <p>currentPage =request.getParameter(“currentPage”); </p> <p>from =request.getParameter(“from”); </p> <p>pactdata = request.getParameter(“modelc.pactdata”); </p> <p>voyagename = request.getParameter(“modela.voyagename”); </p> <p>voyageno = request.getParameter(“modela.voyageno”); </p> <p>dwt = request.getParameter(“modelc.dwt”); </p> <p>hirefrom = request.getParameter(“modelc.hirefrom”); </p> <p>deliveryposion = request.getParameter(“modela.deliveryposion”); </p> <p>redeliveryposion = request.getParameter(“modela.redeliveryposion”); </p> <p>sheepowner = request.getParameter(“modelc.sheepowner”); </p> <p>addr = request.getParameter(“modelc.addr”); </p> <p>addcomm = request.getParameter(“modelc.addcomm”); </p> <p>rent = request.getParameter(“modelc.rent”); </p> <p>if(type!=null&&type.trim().equals(“1”)){ </p> <p>type =”已还船舶–费用未结清”; </p> <p>}else{ </p> <p>type =”已还船舶–费用已结清”; </p> <p>} </p> <p>property =getStr(pactdata,cell)+getStr(voyagename,cell)+getStr(voyageno,cell)+getStr(dwt,cell)+getStr(hirefrom,cell) </p> <p>+getStr(deliveryposion,cell)+getStr(redeliveryposion,cell)+getStr(sheepowner,cell)+getStr(addr,cell)+getStr(addcomm,cell) </p> <p>+getStr(rent,cell); </p> <p>property = property.substring(1); </p> <p>String split = property.split(“,”); </p> <p>// System.out.println(“-property:”+property); </p> <p>if(currentPage!=null&&!currentPage.trim().equals(“”)){ </p> <p>ncurrentPage =Integer.parseInt(currentPage); </p> <p>}else{ </p> <p>OutJavaScript.outString(response, “Sorry! Failed to get information of pager.”); </p> <p>return null; </p> <p>} </p> <p>try { </p> <p>session =getServiceLocator().getBaseHibernateDAO().getSession(); </p> <p>sql =”select count(*) “+from; </p> <p>query =session.createQuery(sql); </p> <p>list = query.list(); </p> <p>for (int i = 0; i </p> <p>totalSize =(Integer)list.get(i); </p> <p>if(totalSize!=0){ </p> <p>pager =new Pager(ncurrentPage,totalSize); </p> <p>} </p> <p>} </p> <p>query =getServiceLocator().getBaseHibernateDAO().getSession().createQuery(“select ” +property+from); </p> <p>if(condition!=null&&condition.trim().equals(“1”)){//分页数据 </p> <p>startRow = (ncurrentPage – 1)*pager.getPageSize(); </p> <p>query.setFirstResult(startRow); </p> <p>query.setMaxResults(pager.getPageSize()); </p> <p>// System.out.println(“query:”+query); </p> <p>} </p> <p>list = query.list(); </p> <p>fileName = “shipInfo”; </p> <p>os = response.getOutputStream(); </p> <p>response.reset(); </p> <p>response.setHeader(“Content-disposition”, </p> <p>“attachment; filename=” +fileName + “.xls”); </p> <p>response.setContentType(“application/msexcel”); </p> <p>jxl.write.WritableWorkbook wbook = Workbook.createWorkbook(os); </p> <p>jxl.write.WritableSheet wsheet = wbook.createSheet(“the first sheet”, 0); </p> <p>for (int i = 0; i </p> <p>jxl.write.Label wlabel0; </p> <p>wlabel0 = new jxl.write.Label(i, 0, getExcelTile(split<i>)); </p> <p><p>wsheet.addCell(wlabel0); </p> <p>} </p> <p>jxl.write.Label wlabel1; </p> <p>for(int i=0;i</p> <p>if(split.length==1){ </p> <p>Object strval = (Object) list.get(i); </p> <p>String javaScript=””+MyPublic.toHtmlStr(strval==null?””:strval.toString().trim())+””; </p> <p>wlabel1 = new jxl.write.Label(0, i+1,strval==null?””:strval.toString().trim() ); </p> <p>wsheet.addCell(wlabel1); </p> <p>}else{ </p> <p>Object strval = (Object) list.get(i); </p> <p>for(int j=0;j</p> <p>String javaScript=””+MyPublic.toHtmlStr(strval==null?””:strval.toString().trim())+””; </p> <p><p>//System.out.println(“===================script:”+javaScript); </p> <p>wlabel1 = new jxl.write.Label(j, i+1,strval==null?””:strval.toString().trim() ); </p> <p><p>wsheet.addCell(wlabel1); </p> <p>} </p> <p>} </p> <p>} </p> <p>wbook.write(); </p> <p>response.flushBuffer(); </p> <p>wbook.close(); </p> <p>os.close(); </p> <p>} catch (IOException e) { </p> <p>// TODO Auto-generated catch block </p> <p>OutJavaScript.outString(response, “Sorry! Export Excel exception.”); </p> <p>e.printStackTrace(); </p> <p>} catch (HibernateException e1) { </p> <p>// TODO Auto-generated catch block </p> <p>OutJavaScript.outString(response, “Sorry! Database exception.”); </p> <p>e1.printStackTrace(); </p> <p>} catch (AppSystemException e1) { </p> <p>// TODO Auto-generated catch block </p> <p>OutJavaScript.outString(response, “Sorry! System exception.”); </p> <p>e1.printStackTrace(); </p> <p>} catch (AppBusinessException e1) { </p> <p>// TODO Auto-generated catch block </p> <p>OutJavaScript.outString(response, “Sorry! Database exception.”); </p> <p>e1.printStackTrace(); </p> <p>} catch (RowsExceededException e) { </p> <p>// TODO Auto-generated catch block </p> <p>OutJavaScript.outString(response, “Sorry! Export Excel exception.”); </p> <p>e.printStackTrace(); </p> <p>} catch (WriteException e) { </p> <p>// TODO Auto-generated catch block </p> <p>OutJavaScript.outString(response, “Sorry! Export Excel exception.”); </p> <p>e.printStackTrace(); </p> <p>} </p> <p>return null; </p> <p>} </p> <p>@Override </p> <p>public ActionForward update(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { </p> <p>// TODO Auto-generated method stub </p> <p>return null; </p> <p>} </p> <p>}</p> <p>还有其他很多种 字数限制 无法一一举例方式</p> <p>这个好像不能实现吧,导出的应该都是文本的!</p> <h3 id="如何从java输出到excel">如何从java输出到excel</h3> <p>用JAVA程序,读取或者写入excel文件戚岁,通过用jxl或者poi,下面是我给你写的例子。分别是用jxl读写excel文件,用poi读写excel文件。希望对你有帮助。(需要下载jxl和poi的jar包仿蚂)</p> <p>package util.excel;</p> <p>import java.io.FileInputStream;</p> <p>import java.io.FileOutputStream;</p> <p>import java.io.IOException;</p> <p>import java.util.ArrayList;</p> <p>import java.util.HashMap;</p> <p>import java.util.List;</p> <p>import java.util.Map;</p> <p>import jxl.Cell;</p> <p>import jxl.Sheet;</p> <p>import jxl.Workbook;</p> <p>import jxl.format.Colour;</p> <p>import jxl.format.UnderlineStyle;</p> <p>import jxl.write.Label;</p> <p>import jxl.write.WritableCellFormat;</p> <p>import jxl.write.WritableFont;</p> <p>import jxl.write.WritableSheet;</p> <p>import jxl.write.WritableWorkbook;</p> <p>import org.apache.poi.hssf.usermodel.HSSFCell;</p> <p>import org.apache.poi.hssf.usermodel.HSSFRichTextString;</p> <p>import org.apache.poi.hssf.usermodel.HSSFRow;</p> <p>import org.apache.poi.hssf.usermodel.HSSFSheet;</p> <p>import org.apache.poi.hssf.usermodel.HSSFWorkbook;</p> <p>import org.apache.poi.poifs.filesystem.POIFSFileSystem;</p> <p>public class ExcelUtil {</p> <p> /**</p> <p> * @param args</p> <p> * @throws IOException</p> <p> */</p> <p> public static void main(String args) throws IOException {</p> <p> String outFile = “D:/workspace/JavaStudy/src/util/excel/test.xls”;</p> <p> ExcelUtil.writeExcelByJXL(outFile, null);</p> <p> }</p> <p> /**</p> <p> * </p> <p> * @title: readExcelByJXL</p> <p> * @description: 通过jxl读取excel文件</p> <p> * @author yu ren tian </p> <p> * @email </p> <p> * @param excelFile</p> <p> * @return</p> <p> * @throws IOException</p> <p> */</p> <p> private static List readExcelByJXL(String excelFile) throws IOException {</p> <p> List rtn = new ArrayList();</p> <p> FileInputStream fileInputStream = null;</p> <p> try {</p> <p> fileInputStream = new FileInputStream(excelFile);</p> <p> Workbook excelWorkBook = Workbook.getWorkbook(fileInputStream);</p> <p> Sheet sheet = excelWorkBook.getSheet(0);</p> <p> int m = sheet.getRows();</p> <p> int n = sheet.getColumns();</p> <p> for (int i = 1; i </p> <p>function AllAreaExcel() { </p> <p>var title;</p> <p>title=document.getElementsByTagName(“table”).childNodes.item(0).childNodes(0).childNodes(0).innerText;</p> <p><p>alert(title);</p> <p>var oXL = new ActiveXObject(“Excel.Application”); </p> <p>var oWB = oXL.Workbooks.Add(); </p> <p>var oSheet = oWB.ActiveSheet; </p> <p>//从excel的第5行开始插入</p> <p>oSheet.Range(“A5”).select; </p> <p>oWB .Worksheets(1).Activate;</p> <p>oSheet.Cells(3,1).Value=title; //在第3行插入报表头</p> <p>oWB.Worksheets(1).Range(“A3:I3”).merge(); // 合并单元格区域 A3:I3</p> <p>oWB.Worksheets(1).Range(“亩掘A3:I3”).HorizontalAlignment=3; //旁或居中对齐A3:I3</p> <p>var sel=document.body.createTextRange();</p> <p>sel.moveToElementText(table1); //table 的ID值</p> <p>sel.select();</p> <p>sel.execCommand(“Copy”);</p> <p>oSheet.Paste(); </p> <p>oXL.Visible = true;</p> <h3 id="Java怎样将数据库中数据导出为Excel文件,求完整例子附带数据库。">Java怎样将数据库中数据导出为Excel文件,求完整例子附带数据库。</h3> <p>用POI啊!</p> <p>感觉挺好使的,刚刚做完一个账目表导出。</p> <p>下面是我写的一个运用jxl生成Excel文件的方法</p> <p>该方法接收一个保存MarkesData数据的ArrayList arlist和文件生成路径Path</p> <p>通过取得arlist里的数据生成Excel文件</p> <p>感觉应尺知该是你想要的 我没调试过也许有错 你也可以根据你的需要进握穗行修改^_^</p> <p>import java.io.File;</p> <p>import java.io.IOException;</p> <p>import java.util.ArrayList;</p> <p>import java.util.Iterator;</p> <p>import jxl.Workbook;</p> <p>import jxl.write.Label;</p> <p>import jxl.write.WritableSheet;</p> <p>import jxl.write.WritableWorkbook;</p> <p>import jxl.write.WriteException;</p> <p>import jxl.write.biff.RowsExceededException;</p> <p>public class WriteExcel {</p> <p>WritableWorkbook book=null;</p> <p>public void OutputExcel(ArrayList arlist,String Path){</p> <p> try{</p> <p>book = Workbook.createWorkbook(new File(Path));</p> <p>//设置表名</p> <p>WritableSheet sheet = book.createSheet(“考试单”,0);</p> <p>//生成表格题头</p> <p>Label labe1 = new Label(0, 0, “考生姓名” );</p> <p>Label labe2 = new Label(1, 0, “地区”);</p> <p>Label labe3 = new Label(2, 0, “所属院校”);</p> <p>Label labe4 = new Label(3, 0, “班级”);</p> <p>Label labe5 = new Label(4, 0, “考试号”);</p> <p>Label labe6 = new Label(5, 0, “考试时间”);</p> <p>Label labe7 = new Label(6, 0, “科目名称”);</p> <p>//将生成的段困卜单元格添加到工作表中 </p> <p>sheet.addCell(labe1);</p> <p>sheet.addCell(labe2);</p> <p>sheet.addCell(labe3);</p> <p>sheet.addCell(labe4);</p> <p>sheet.addCell(labe5);</p> <p>sheet.addCell(labe6);</p> <p>sheet.addCell(labe7);</p> <p>Iterator it = arlist.iterator();</p> <p>int i = 1;</p> <p> while(it.hasNext()){</p> <p>//通过迭代获得arlist里的MarkesData对象</p> <p>MarkesData temp = (MarkesData)it.next();</p> <p>//取得数据生成单元格</p> <p>Label label1=new Label(0,i,temp.getUser_name());</p> <p>Label label2=new Label(1,i,temp.getArea_name());</p> <p>Label label3=new Label(2,i,temp.getCollege_name());</p> <p>Label label4=new Label(3,i,temp.getClass_name());</p> <p>Label label5=new Label(4,i,temp.getTest_name());</p> <p>Label label6=new Label(5,i,temp.getStarttime());</p> <p>Label label7=new Label(6,i,temp.getSubject_name());</p> <p>//将生成的单元格添加到工作表中 </p> <p>sheet.addCell(label1);</p> <p>sheet.addCell(label2);</p> <p>sheet.addCell(label3);</p> <p>sheet.addCell(label4);</p> <p>sheet.addCell(label5);</p> <p>sheet.addCell(label6);</p> <p>sheet.addCell(label7); </p> <p>i++;</p> <p>}</p> <p>book.write(); </p> <p>book.close();</p> <p>} catch (RowsExceededException e) { </p> <p>e.printStackTrace(); </p> <p>} catch (WriteException e) { </p> <p>e.printStackTrace(); </p> <p>} catch (IOException e) { </p> <p>e.printStackTrace(); </p> <p>} finally{</p> <p>try{</p> <p> if(book!=null)book.close();</p> <p>}catch(Exception e){</p> <p> System.out.println(“exception when closing Connection in finally”);</p> <p> System.out.println(e.getMessage().toString());</p> <p>}</p> <p>}</p> <p> }</p> <p>}</p> <p>java导出数据库数据为excel的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java导出数据库数据为excel,用Java轻松实现数据库数据导出为Excel表格,java导出excel,如何从java输出到excel,Java怎样将数据库中数据导出为Excel文件,求完整例子附带数据库。的信息别忘了在本站进行查找喔。</p> </div> <div class=""> <p><strong>相关文章</strong></p> </div> </article> </div> </main> <footer> <div class="container"> <p> <span>友情链接:</span> <a href="https://www.688576.com" target="_blank">雨伦博客</a>   <a href="https://www.yaanbbs.net" target="_blank">雅安论坛</a> </p> <a href="https://beian.miit.gov.cn" target="_blank">京ICP备15023317号-6</a> </div> </footer> <script> var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?30b42218aa13759c43de5f1971d0a93b"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); </script> </body> </html>