一段python代码:已知excel名为《文件名》,sheet名为《testsheet》,第一列是开始时间,格式为年月日时分秒,第二列是结束时间,格式为年月日时分秒,第三列的时间等于第一列到第二列的时间差,格式为时分秒。
时间: 2023-05-26 16:04:11 浏览: 85
import pandas as pd
# 读取Excel文件,指定sheet名称
df = pd.read_excel("文件名.xlsx", sheet_name="testsheet")
# 计算时间差并添加为新列
df["时间差"] = df["结束时间"] - df["开始时间"]
# 将时间差转换为时分秒格式
df["时间差"] = df["时间差"].apply(lambda x: str(x).split(":"))
for i in range(len(df)):
hours = int(df.loc[i, "时间差"][0])
minutes = int(df.loc[i, "时间差"][1])
seconds = int(df.loc[i, "时间差"][2])
df.loc[i, "时间差"] = "{:02d}:{:02d}:{:02d}".format(hours, minutes, seconds)
# 打印结果
print(df)
相关问题
easyExcel 的边框加粗
### 如何在 EasyExcel 中设置单元格边框加粗
为了实现单元格边框加粗的效果,在 `CustomCellWriteHandler` 类中可以重写 `onCellCreated` 方法来定制化单元格样式。通过调用 `CellStyleUtil.setBorder` 方法并传递相应的参数,能够指定边框的宽度和其他属性。
下面是一个具体的例子展示如何创建带有加粗边框样式的 Excel 文件:
```java
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.write.handler.AbstractCellWriteHandler;
import org.apache.poi.ss.usermodel.*;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) throws Exception {
// 指定输出的Excel文件路径
String excelFilePath = "example-write.xlsx";
// 设置要输出的数据内容
List<Person> personList = new ArrayList<>();
personList.add(new Person("a01", "张三", "男", 10));
personList.add(new Person("a02", "李四", "男", 20));
personList.add(new Person("a03", "王五", "女", 30));
CustomCellWriteHandler customCellWriteHandler = new CustomCellWriteHandler();
EasyExcel.write(excelFilePath, Person.class)
.registerWriteHandler(customCellWriteHandler)
.sheet("testSheet")
.doWrite(personList);
}
}
class CustomCellWriteHandler extends AbstractCellWriteHandler {
@Override
protected void onCellCreated(Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
super.onCellCreated(cell, head, relativeRowIndex, isHead);
Workbook workbook = cell.getSheet().getWorkbook();
CellStyle style = workbook.createCellStyle();
Font font = workbook.createFont();
style.setFont(font);
// 创建带加粗边框的样式
setBorder(style, BorderStyle.MEDIUM, IndexedColors.BLACK.getIndex());
cell.setCellStyle(style);
}
private void setBorder(CellStyle style, short borderStyle, short borderColor) {
style.setBorderTop(borderStyle);
style.setTopBorderColor(borderColor);
style.setBorderBottom(borderStyle);
style.setBottomBorderColor(borderColor);
style.setBorderLeft(borderStyle);
style.setLeftBorderColor(borderColor);
style.setBorderRight(borderStyle);
style.setRightBorderColor(borderColor);
}
}
```
上述代码展示了怎样利用 `setBorder` 函数给选定区域内的所有边界应用相同的样式[^3]。
阅读全文