gpt4 book ai didi

java - 使用Java更新Excel文件(插入行)

转载 作者:太空宇宙 更新时间:2023-11-04 10:15:07 27 4
gpt4 key购买 nike

我正在尝试使用java更新Excel文件。

我们有多台服务器,每周五需要检查其运行状况,即磁盘空间,通过登录这些服务器并检查大小,并删除一些早于一个月的文件..然后填写有关磁盘空间的 Excel,然后注销。

但现在我正在尝试使用 Java 自动执行任务。我成功检查了磁盘大小并删除了一个月以上的文件,并将该数据放入 Excel 文件中。

public class UpdateExcel 
{
public static void main(String[]args) throws
EncryptedDocumentException, InvalidFormatException, IOException
{
File f=new File("D:\\TestDeleteFiles");
String list[]=f.list();
Calendar lCal = Calendar.getInstance();
lCal.add(Calendar.DATE, -30);
for(int i=0;i<list.length;i++)
{
System.out.println(list[i]);
File tmpFile=new File("D:\\TestDeleteFiles\\"+list[i]);
Date date=new Date(tmpFile.lastModified());
if(date.before(lCal.getTime()))
{
tmpFile.delete();
System.out.println("Deleted");
}
}
try {
FileSystemView fsv = FileSystemView.getFileSystemView();
File[] drives = File.listRoots();

File cDrive = new File("C:\\");
File dDrive = new File("D:\\");

System.out.println("Drives are : " +cDrive);
System.out.println("Drives are : " +dDrive);

long cTotSpace = cDrive.getTotalSpace();
long cFrSpace = cDrive.getFreeSpace();
long cTotalSpace;
long cFreeSpace;

long dTotSpace = dDrive.getTotalSpace();
long dFrSpace = dDrive.getFreeSpace();
long dTotalSpace;
long dFreeSpace;

cTotalSpace = cTotSpace / (1024*1024*1024);
cFreeSpace = cFrSpace / (1024*1024*1024);

dTotalSpace = dTotSpace / (1024*1024*1024);
dFreeSpace = dFrSpace / (1024*1024*1024);

System.out.println("Total Space in Gb : " + cTotalSpace + " GB");
System.out.println("Free Space in Gb : " + cFreeSpace + " GB");

System.out.println("Total Space in Gb : " + dTotalSpace + " GB");
System.out.println("Free Space in Gb : " + dFreeSpace + " GB");

String excelFilePath = "D:\\CreateXls\\ServerTestDemo.xls";
FileInputStream inputStream = new FileInputStream(new File(excelFilePath));
Workbook workbook = WorkbookFactory.create(inputStream);
Sheet sheet = workbook.getSheetAt(0);

Iterator<Row> iterator = sheet.iterator();
while(iterator.hasNext())
{
Row currentRow = iterator.next();

Cell cell = currentRow.createCell(currentRow.getLastCellNum(), CellType.STRING);
Cell dcell = currentRow.createCell(currentRow.getLastCellNum(), CellType.STRING);
Cell cell1 = currentRow.createCell(currentRow.getLastCellNum(), CellType.STRING);
System.out.println("Hello" + currentRow.getRowNum());
if(currentRow.getRowNum() == 0)
System.out.println("Hello" + currentRow.getRowNum());
cell.setCellValue(cFreeSpace);
dcell.setCellValue(dFreeSpace);
cell1.setCellValue("");
}
inputStream.close();
FileOutputStream outputStream = new
FileOutputStream("D:\\CreateXls\\ServerTestDemo.xls");
workbook.write(outputStream);
workbook.close();
outputStream.close();
} catch (IOException | EncryptedDocumentException
| InvalidFormatException ex) {
ex.printStackTrace();
}
}
}

此代码可以很好地将磁盘大小放入 Excel 文件中。这张图片会告诉您

enter image description here

但现在我想做的是将服务器名称等标签放在 IP 地址上方的第一行,以及基于何时执行该代码的日期以及可用空间上方的 C 或 D 驱动器。

好吧,我尝试过,但由于迭代器服务器名称也打印了多次,这是我不想要的。

<小时/>

注意: 对于信息,我并没有被困在如何获取日期和所有内容上,只是我被困在如何在第一、第二和第三行中放置标签而不重复这是演示,这就是我采用相同 IP 地址的原因

最佳答案

我不清楚你在问什么,也许如果你展示了你想要的 Excel 工作表,那么有人可以帮助你。

根据您的描述,我假设您希望第 1 行有标题,而且您似乎正在使用 Apache POI。如果是这种情况,您可能需要阅读这篇文章:How to add header column in excel using Java Apache POI?

关于java - 使用Java更新Excel文件(插入行),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51820693/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com