cell.setcelltype过时
时间: 2023-04-27 20:03:28 浏览: 977
cell.setcelltype过时是因为在新版本的Excel中,该方法已经被废弃,不再建议使用。取而代之的是使用cell.setcellvalue方法来设置单元格的值。这个方法可以根据单元格的数据类型自动设置单元格的类型。如果需要手动设置单元格的类型,可以使用cell.setcellstyle方法来设置单元格的样式。
相关问题
为什么C#的NPOI使用了以下string strs = $"ROUNDUP(S{Lastrow} + 0.1, ) - S{Lastrow} - 0.1 + S{Lastrow - 1}"; ICell cell = sheetTd.GetRow(Lastrow).GetCell(19) ?? sheetTd.GetRow(Lastrow).CreateCell(19); cell.SetCellType(CellType.Formula); cell.SetCellFormula(strs);写了生成的excel没有公式
这个问题可能是因为您的公式字符串格式不正确导致的。NPOI中使用的公式字符串应该符合Excel的公式语法,如果公式字符串格式不正确,Excel将无法识别它并将其视为普通文本。
在您提供的代码中,公式字符串的格式似乎是不完整的,ROUNDUP函数的第二个参数缺失,这可能会导致Excel无法正确解析这个公式。您可以尝试修改公式字符串,添加缺失的参数,例如:
```
string strs = $"ROUNDUP(S{Lastrow} + 0.1, 0) - S{Lastrow} - 0.1 + S{Lastrow - 1}";
```
另外,您也可以尝试在生成Excel文件后手动打开文件并尝试重新计算公式,看看公式是否被正确地解析并计算。
public static List<Map<String, String>> getAllData(String path, Integer sheetIndex, Integer startIndex, Map<Integer, String> typeIndex) throws IOException { Workbook workBook = getWorkBook(path); Sheet sheet = getSheet(workBook, sheetIndex); ArrayList<Map<String, String>> maps = new ArrayList<>(); for (int i = startIndex; i < sheet.getLastRowNum() + 1; i++) { Row row = sheet.getRow(i); HashMap<String, String> map = new HashMap<>(); for (Map.Entry<Integer, String> integerStringEntry : typeIndex.entrySet()) { Cell cell = row.getCell(integerStringEntry.getKey()); cell.setCellType(CellType.STRING); map.put(integerStringEntry.getValue(), cell.toString()); } maps.add(map); } return maps; }
这段代码是在做什么的?
这段代码是在获取Excel文件中的数据。
它包含了一个方法 `getAllData`,它有四个参数:
- `path`:Excel文件的路径
- `sheetIndex`:要读取的工作表的索引
- `startIndex`:要读取的数据的开始行的索引
- `typeIndex`:每一列对应的数据类型的映射
这个方法使用 Apache POI 库来读取 Excel 文件,然后遍历从 `startIndex` 开始的每一行,将每一行的每一列的数据存储到一个 `Map` 对象中,最后将所有的 `Map` 对象存储到一个 `List` 对象中,并返回这个 `List` 对象。