- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试重命名现有 xlsx 文件的标题。这个想法是用一个 excel 文件将数据从 XML 导出到 excel,并在某些用户进行调整后重新导入 XML。
目前,我们已经使用 Excel 创建了一个"template"xlsx 表,其中已经包含一个可排序表(poi 中的 XSSFTable)和一个到 XSD 源的映射。然后我们通过POI将其导入,将XML数据映射到其中并保存。为了根据用户调整工作表,我们希望将现有表格的标题/列名翻译成不同的语言。它适用于 POI 3.10-FINAL,但自从升级到 4.0.1 后,它会在打开时导致损坏的 xlsx 文件。
我已经在 stackoverflow 上找到了这个问题 Excel file gets corrupted when i change the value of any cell in the header (Columns Title)但它没有回答而且很旧。但我试图弄清楚评论可能是关于什么的,并试图展平现有的 XSSFTable,将填充的数据复制到新工作表并将新的 XSSFTable 放在数据上。可悲的是,这似乎非常复杂,所以我又回来纠正损坏的标题单元格。我还尝试使用 POI 创建整个工作表并放弃使用该"template"-xslx,但我无法弄清楚如何实现我们的 XSD 映射(在 Excel 中,其开发人员工具 -> 源代码 -> 添加然后映射动态表中某些单元格的节点)
poi升级之前一直有效的代码基本是这样的:
//Sheet is the current XSSFSheet
//header is a Map with the original header-name from the template mapped to a the new translated name
//headerrownumber is the row containing the tableheader to be translated
public static void translateHeaders(Sheet sheet,final Map<String,String> header,int headerrownumber) {
CellRangeAddress address = new CellRangeAddress(headerrownumber,headerrownumber,0,sheet.getRow(headerrownumber).getLastCellNum()); //Cellrange is the header-row
MyCellWalk cellWalk = new MyCellWalk (sheet,address);
cellWalk.traverse(new CellHandler() {
public void onCell(Cell cell, CellWalkContext ctx) {
String val = cell.getStringCellValue();
if (header.containsKey(val)) {
cell.setCellValue(header.get(val));
}
}
});
}
MyCellWalk 是一个 org.apache.poi.ss.util.cellwalk.CellWalk,它遍历从左上角到右下角的单元格范围。
据我所知,仅仅更改单元格的平面值是不够的,因为 xlsx 在他们的一些 map 中保留了对单元格名称的引用,但我不知道如何获取它们并重命名标题。也许还有另一种翻译标题名称的方法?
最佳答案
嗯,XSSFTable.updateHeaders如果 apache poi
应该可以解决问题不会失败的。
以下所有操作都是使用 apache poi 4.0.1
完成的.
我已经下载了你的dummy_template.xlsx
然后尝试更改工作表中的表格列标题。但即使在调用 XSSFTable.updateHeaders
之后XSSFTable
中的列名没有改变。所以我看了一下 XSSFTable.java -> updateHeaders以确定为什么这不会发生。我们发现:
if (row != null && row.getCTRow().validate()) {
//do changing the column names
}
因此,只有当工作表中的相应行有效时,列名才会更改 XML
根据 Office Open XML
namespace 。但在以后Excel
版本(2007 年之后)添加了额外的 namespace 。在本例中,该行的 XML
看起来像:
<row r="4" spans="1:3" x14ac:dyDescent="0.25">
注意附加的 x14ac:dyDescent
属性。这就是为什么 row.getCTRow().validate()
返回 false
.
以下代码获取您的 dummy_template.xlsx
, 重命名工作表中的列标题,然后调用解除武装的版本 static void updateHeaders(XSSFTable table)
.之后 result.xlsx
有效于 Excel
开业.
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import org.apache.poi.ss.util.cellwalk.*;
import org.apache.poi.xssf.usermodel.*;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
import java.io.*;
import java.util.*;
class ExcelRenameTableColumns {
static void translateHeaders(Sheet sheet, final Map<String,String> header, int headerrownumber) {
CellRangeAddress address = new CellRangeAddress(
headerrownumber, headerrownumber,
0, sheet.getRow(headerrownumber).getLastCellNum());
CellWalk cellWalk = new CellWalk (sheet, address);
cellWalk.traverse(new CellHandler() {
public void onCell(Cell cell, CellWalkContext ctx) {
String val = cell.getStringCellValue();
if (header.containsKey(val)) {
cell.setCellValue(header.get(val));
}
}
});
}
static void updateHeaders(XSSFTable table) {
XSSFSheet sheet = (XSSFSheet)table.getParent();
CellReference ref = table.getStartCellReference();
if (ref == null) return;
int headerRow = ref.getRow();
int firstHeaderColumn = ref.getCol();
XSSFRow row = sheet.getRow(headerRow);
DataFormatter formatter = new DataFormatter();
System.out.println(row.getCTRow().validate()); // false!
if (row != null /*&& row.getCTRow().validate()*/) {
int cellnum = firstHeaderColumn;
CTTableColumns ctTableColumns = table.getCTTable().getTableColumns();
if(ctTableColumns != null) {
for (CTTableColumn col : ctTableColumns.getTableColumnList()) {
XSSFCell cell = row.getCell(cellnum);
if (cell != null) {
col.setName(formatter.formatCellValue(cell));
}
cellnum++;
}
}
}
}
public static void main(String[] args) throws Exception {
String templatePath = "dummy_template.xlsx";
String outputPath = "result.xlsx";
FileInputStream inputStream = new FileInputStream(templatePath);
Workbook workbook = WorkbookFactory.create(inputStream);
Sheet sheet = workbook.getSheetAt(0);
Map<String, String> header = new HashMap<String, String>();
header.put("textone", "Spalte eins");
header.put("texttwo", "Spalte zwei");
header.put("textthree", "Spalte drei");
translateHeaders(sheet, header, 3);
XSSFTable table = ((XSSFSheet)sheet).getTables().get(0);
updateHeaders(table);
FileOutputStream outputStream = new FileOutputStream(outputPath);
workbook.write(outputStream);
outputStream.close();
workbook.close();
}
}
如果我打开 dummy_template.xlsx
使用 Excel 2007
然后另存为dummy_template2007.xlsx
, 该行的 XML
更改为
<row r="4" spans="1:3">
现在使用这个 dummy_template2007.xlsx
无需手动调用 XSSFTable.updateHeaders
是必要的。 XSSFTable.writeTo由 XSSFTable.commit
调用自动执行此操作。
关于java - 使用 Apache Poi 重命名 XSSFTable 的 header 会导致损坏的 XLSX 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55532006/
我正在尝试使用 Spark 从 Cassandra 读取数据。 DataFrame rdf = sqlContext.read().option("keyspace", "readypulse
这是代码: void i_log_ (int error, const char * file, int line, const char * fmt, ...) { /* Get erro
我必须调试一个严重依赖 Gtk 的程序。问题是由于某些原因,在使用 GtkWindow 对象时开始出现许多运行时警告。问题是,即使 Gtk 提示严重错误,它也不会因这些错误而中止。我没有代码库的更改历
我正在尝试从已有效编译和链接的程序中检索二进制文件。我已经通过 GL_PROGRAM_BINARY_LENGTH 收到了它的长度。该文档说有两个实例可能会发生 GL_INVALID_OPERATION
我有一个托管在 Azure 环境中的服务。我正在使用控制台应用程序使用该服务。这样做时,我得到了异常: "The requested service, 'http://xxxx-d.yyyy.be/S
我有以下代码,它被 SEGV 信号杀死。使用调试器表明它被 main() 中的第一个 sem_init() 杀死。如果我注释掉第一个 sem_init() ,第二个会导致同样的问题。我试图弄清楚是什么
目前我正在编写一个应用程序(目标 iOS 6,启用 ARC),它使用 JSON 进行数据传输,使用核心数据进行持久存储。 JSON 数据由 PHP 脚本通过 json_encode 从 MySQL 数
我对 Xamarin.Forms 还是很陌生。我在出现的主页上有一个非常简单的功能 async public Task BaseAppearing() { if (UserID
这是我的代码的简化版本。 public class MainActivity extends ActionBarActivity { private ArrayList entry = new Arr
我想弄明白为什么我的两个 Java 库很难很好地协同工作。这是场景: 库 1 有一个类 A,其构造函数如下: public A(Object obj) { /* boilerplate */ } 在以
如果网站不需要身份验证,我的代码可以正常工作,如果需要,则在打印“已创建凭据”后会立即出现 EXC_BAD_ACCESS 错误。我不会发布任何内容,并且此代码是直接从文档中复制的 - 知道出了什么问题
我在使用 NSArray 填充 UITableView 时遇到问题。我确信我正在做一些愚蠢的事情,但我无法弄清楚。当我尝试进行简单的计数时,我得到了 EXC_BAD_ACCESS,我知道这是因为我试图
我在 UITableViewCell 上有一个 UITextField,在另一个单元格上有一个按钮。 我单击 UITextField(出现键盘)。 UITextField 调用了以下方法: - (BO
我有一个应用程序出现间歇性崩溃。崩溃日志显示了一个堆栈跟踪,这对我来说很难破译,因此希望其他人看到了这一点并能为我指出正确的方向。 基本上,应用程序在启动时执行反向地理编码请求,以在标签中显示用户的位
我开发了一个 CGImage,当程序使用以下命令将其显示在屏幕上时它工作正常: [output_view.layer performSelectorOnMainThread:@selector(set
我正在使用新的 EncryptedSharedPreferences以谷歌推荐的方式上课: private fun securePrefs(context: Context): SharedPrefe
我有一个中继器,里面有一些控件,其中一个是文本框。我正在尝试使用 jquery 获取文本框,我的代码如下所示: $("#").click(function (event) {}); 但我总是得到 nu
在以下场景中观察到 TTS 初始化错误,太随机了。 已安装 TTS 引擎,存在语音集,并且可以从辅助功能选项中播放示例 tts。 TTS 初始化在之前初始化和播放的同一设备上随机失败。 在不同的设备(
maven pom.xml org.openjdk.jol jol-core 0.10 Java 类: public class MyObjectData { pr
在不担心冲突的情况下,可以使用 MD5 作为哈希值,字符串长度最多为多少? 这可能是通过为特定字符集中的每个可能的字符串生成 MD5 哈希来计算的,长度不断增加,直到哈希第二次出现(冲突)。没有冲突的
我是一名优秀的程序员,十分优秀!