java在线编辑excel
时间: 2023-08-22 09:04:29 浏览: 51
你可以使用Apache POI库来在Java中编辑Excel文件。以下是一个简单的示例代码,可以创建一个工作簿并在其中创建一个名为“Sheet1”的工作表,并在第一行中添加一些数据:
```
import java.io.FileOutputStream;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelEditor {
public static void main(String[] args) throws Exception {
// Create a new workbook
Workbook workbook = new XSSFWorkbook();
// Create a new sheet
Sheet sheet = workbook.createSheet("Sheet1");
// Create a header row
Row headerRow = sheet.createRow(0);
// Add some headers
headerRow.createCell(0).setCellValue("Name");
headerRow.createCell(1).setCellValue("Age");
headerRow.createCell(2).setCellValue("Email");
// Add some data
Row dataRow = sheet.createRow(1);
dataRow.createCell(0).setCellValue("John Doe");
dataRow.createCell(1).setCellValue(30);
dataRow.createCell(2).setCellValue("johndoe@example.com");
// Save the workbook
FileOutputStream fileOut = new FileOutputStream("example.xlsx");
workbook.write(fileOut);
fileOut.close();
// Close the workbook
workbook.close();
}
}
```
这个示例代码使用了XSSFWorkbook类来创建一个新的工作簿,然后使用createSheet方法创建一个新的工作表。然后,使用createRow方法在工作表中创建一个新的行,并使用createCell方法在行中创建单元格。最后,使用FileOutputStream将工作簿保存到文件中。
你可以使用类似的方法来读取现有的Excel文件并编辑它们。
相关推荐
![](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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)