帮我用JAVA写一个表格
时间: 2023-10-13 15:06:18 浏览: 48
好的,以下是一个简单的 Java 代码片段,用于创建一个基本的表格:
```java
import javax.swing.*;
import javax.swing.table.*;
public class MyTable extends JFrame {
public MyTable() {
JTable table = new JTable(new DefaultTableModel(
new Object[][]{{"A", "B"}, {"C", "D"}},
new Object[]{"Column 1", "Column 2"}));
JScrollPane scrollPane = new JScrollPane(table);
add(scrollPane);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(300, 200);
setVisible(true);
}
public static void main(String[] args) {
new MyTable();
}
}
```
这个程序创建了一个包含两列的简单表格,每列包含两个单元格。你可以根据需要修改数据和列标题。
相关问题
用Java写一个Excel 表格导入
可以使用Apache POI库来操作Excel文件。以下是一个简单的示例代码,演示如何使用Java读取Excel文件并打印出其中的内容:
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
public class ExcelReader {
public static void main(String[] args) {
try {
// 读取Excel文件
FileInputStream file = new FileInputStream(new File("example.xlsx"));
Workbook workbook = WorkbookFactory.create(file);
// 选择第一个Sheet
Sheet sheet = workbook.getSheetAt(0);
// 遍历每一行
for (Row row : sheet) {
// 遍历每一列
for (Cell cell : row) {
// 打印单元格的值
System.out.print(cell.getStringCellValue() + "\t");
}
System.out.println();
}
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
需要注意的是,该示例代码需要在项目中引入Apache POI库的相关依赖。
用java代码写一个表格导出
可以使用Apache POI库来实现表格导出,以下是一个简单的示例代码:
```
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelExporter {
public static void exportTable(String[][] tableData, String filePath) throws IOException {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Table Data");
for (int i = ; i < tableData.length; i++) {
Row row = sheet.createRow(i);
for (int j = ; j < tableData[i].length; j++) {
Cell cell = row.createCell(j);
cell.setCellValue(tableData[i][j]);
}
}
FileOutputStream outputStream = new FileOutputStream(filePath);
workbook.write(outputStream);
workbook.close();
outputStream.close();
}
}
```
其中,tableData是一个二维字符串数组,表示要导出的表格数据;filePath是导出的文件路径。该方法会将表格数据写入一个.xlsx文件中。
相关推荐
![text/x-java](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)