java代码随机创建50条医生信息Excel表
时间: 2023-07-30 17:07:26 浏览: 100
生成Excel表的java代码.docx
好的,以下是一个Java代码随机创建50条医生信息Excel表的示例:
```java
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class CreateDoctorExcel {
public static void main(String[] args) {
try {
// 创建Excel工作簿和工作表
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet("医生信息表");
// 创建表头行
Row headerRow = sheet.createRow(0);
String[] headers = {"姓名", "职称", "科室", "联系方式"};
for (int i = 0; i < headers.length; i++) {
Cell cell = headerRow.createCell(i);
cell.setCellValue(headers[i]);
}
// 随机生成50条医生信息并写入表格
for (int i = 1; i <= 50; i++) {
Row row = sheet.createRow(i);
String[] titles = {"主任医师", "副主任医师", "主治医师", "住院医师"};
String[] departments = {"内科", "外科", "儿科", "妇产科", "眼科", "口腔科", "耳鼻喉科", "皮肤科"};
String name = "医生" + i;
String title = titles[(int) (Math.random() * titles.length)];
String department = departments[(int) (Math.random() * departments.length)];
String phone = "1" + String.format("%010d", (int) (Math.random() * 10000000000L));
row.createCell(0).setCellValue(name);
row.createCell(1).setCellValue(title);
row.createCell(2).setCellValue(department);
row.createCell(3).setCellValue(phone);
}
// 将表格写入文件
FileOutputStream outputStream = new FileOutputStream("doctor.xlsx");
workbook.write(outputStream);
workbook.close();
outputStream.close();
System.out.println("Excel文件创建成功!");
} catch (IOException e) {
e.printStackTrace();
}
}
}
```
该示例使用了Apache POI库来操作Excel文件,随机生成50条医生信息并写入表格。您可以根据需要修改表格的表头、字段、数据类型等等。希望对您有所帮助。
阅读全文