新聞中心
xml,, org.apache.poi , poi , 5.1.0 , ,, org.apache.poi , poi-ooxml , 5.1.0 , ,`,,2. 編寫代碼導(dǎo)出數(shù)據(jù)到Excel:,,`java,import org.apache.poi.ss.usermodel.*;,import org.apache.poi.xssf.usermodel.XSSFWorkbook;,,import java.io.FileOutputStream;,import java.io.IOException;,,public class ExportToExcel {, public static void main(String[] args) {, Workbook workbook = new XSSFWorkbook();, Sheet sheet = workbook.createSheet("數(shù)據(jù)");,, for (int i = 0; i < 5; i++) {, Row row = sheet.createRow(i);, for (int j = 0; j < 5; j++) {, Cell cell = row.createCell(j);, cell.setCellValue("數(shù)據(jù)" + i + "," + j);, }, },, try (FileOutputStream outputStream = new FileOutputStream("導(dǎo)出數(shù)據(jù).xlsx")) {, workbook.write(outputStream);, } catch (IOException e) {, e.printStackTrace();, } finally {, try {, workbook.close();, } catch (IOException e) {, e.printStackTrace();, }, }, },},“,,這段代碼將創(chuàng)建一個(gè)包含5行5列數(shù)據(jù)的Excel文件,并將其保存為”導(dǎo)出數(shù)據(jù).xlsx”。在Java中,我們可以使用Apache POI庫(kù)來創(chuàng)建和操作Excel文件,以下是一個(gè)簡(jiǎn)單的示例,演示如何創(chuàng)建一個(gè)Excel文件,并在其中添加一些數(shù)據(jù)和格式:

成都創(chuàng)新互聯(lián)公司堅(jiān)持“要么做到,要么別承諾”的工作理念,服務(wù)領(lǐng)域包括:網(wǎng)站建設(shè)、網(wǎng)站制作、企業(yè)官網(wǎng)、英文網(wǎng)站、手機(jī)端網(wǎng)站、網(wǎng)站推廣等服務(wù),滿足客戶于互聯(lián)網(wǎng)時(shí)代的平陸網(wǎng)站設(shè)計(jì)、移動(dòng)媒體設(shè)計(jì)的需求,幫助企業(yè)找到有效的互聯(lián)網(wǎng)解決方案。努力成為您成熟可靠的網(wǎng)絡(luò)建設(shè)合作伙伴!
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import java.io.FileOutputStream;
import java.io.IOException;
public class ExportToExcel {
public static void main(String[] args) throws IOException {
// 創(chuàng)建一個(gè)新的Excel工作簿
Workbook workbook = new XSSFWorkbook();
// 創(chuàng)建一個(gè)新的工作表
Sheet sheet = workbook.createSheet("My Sheet");
// 創(chuàng)建行
Row row = sheet.createRow(0);
// 創(chuàng)建單元格并設(shè)置值
Cell cell = row.createCell(0);
cell.setCellValue("Hello, World!");
// 創(chuàng)建另一個(gè)單元格并設(shè)置值
Cell cell2 = row.createCell(1);
cell2.setCellValue("Hello, Java!");
// 創(chuàng)建另一個(gè)單元格并設(shè)置值
Cell cell3 = row.createCell(2);
cell3.setCellValue("Hello, POI!");
// 創(chuàng)建另一個(gè)單元格并設(shè)置值
Cell cell4 = row.createCell(3);
cell4.setCellValue("Hello, Apache!");
// 寫入到文件
FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
workbook.write(fileOut);
fileOut.close();
// 關(guān)閉工作簿
workbook.close();
}
}
在這個(gè)例子中,我們首先創(chuàng)建了一個(gè)新的Excel工作簿,然后在這個(gè)工作簿中創(chuàng)建了一個(gè)新的工作表,我們?cè)诠ぷ鞅碇袆?chuàng)建了一行,并在這一行中創(chuàng)建了四個(gè)單元格,每個(gè)單元格都有一個(gè)值,這些值都是字符串,我們將這個(gè)工作簿寫入到一個(gè)文件中,并關(guān)閉了工作簿。
分享文章:java導(dǎo)出數(shù)據(jù)到excel表格格式
URL鏈接:http://m.5511xx.com/article/djscpdj.html


咨詢
建站咨詢
