如何修复“我们发现 'abc.xlsm' 中的某些内容存在问题。您是否希望我们尝试尽可能多地恢复?
How to fix "We found a problem with some content in 'abc.xlsm'. Do you want us to try to recover as much as we can?
我的代码中有以下方法 (createAdditionalSheetsInExcel
),它尝试为特定场景 (runOutputNumber > 1
) 创建额外的 sheet。它最终会创建一个 excel,但问题是当您尝试打开 excel 时,您最终会收到以下错误:
workBookObj.cloneSheet(index,sheetName)
在 java 代码中没有抛出任何错误,但是当我尝试打开 excel 文件时,出现以下错误:
我尝试删除 sheet 中 table 的格式,然后错误消失了。所以一定是跟sheet里面的table的格式有关。
private static void createAdditionalSheetsInExcel(String tempOutputFileName, String outputFileName, int runOutputNumber) throws IOException {
FileInputStream fileIn = new FileInputStream(tempOutputFileName);
XSSFWorkbook workBookObj = new XSSFWorkbook(fileIn);
workBookObj.setWorkbookType(XSSFWorkbookType.XLSM);
runOutputNumber = 2;//Hard coded for clarification
if (runOutputNumber > 1) {
int initialNoOfSheets = workBookObj.getNumberOfSheets();
for (int runIndex = 2; runIndex <= runOutputNumber; runIndex++) {
for (int index = 0; index < initialNoOfSheets; index++) {
XSSFSheet sheet = workBookObj.getSheetAt(index);
String sheetName = sheet.getSheetName().trim()
.substring(0, sheet.getSheetName().length() - 1) + runIndex;
workBookObj.cloneSheet(index, sheetName);
}
}
}
FileOutputStream fileOut = new FileOutputStream(outputFileName);
workBookObj.write(fileOut);
fileOut.close();
workBookObj.close();
deleteTempExcel(tempOutputFileName);
}
excel 尝试打开时出错:
我们发现 'abc.xlsm' 中的某些内容存在问题。你想尽可能多地恢复吗?如果您信任此工作簿的来源,请单击“是”。
错误:打开 excel 文件后:
已修复记录:Table 来自 /xl/tables/table1。xml 部分 (Table)
最终使用 jacob api 以及对模板进行更改解决了该问题。问题出在 Excel 中定义的局部变量,我可以通过转到(公式 -> 名称管理器)访问它并在删除变量后删除 variable.Even 我无法让它与 Apache 一起工作POI 因此最终使用了 Jacob api。代码如下:
package com.ford.ltdrive.model.output.excel.excelenum;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
public class CloneSheet {
public void backup(String filepath) {
try {
Date d = new Date();
String dateString = (d.getYear() + 1900) + "_" + d.getMonth() + "_" + d.getDate();
// String backupfilepath = filepath.replace(".xlsm", "_backup_" + dateString + ".xlsm");
//Path copied = Paths.get(backupfilepath);
Path copied1 = Paths.get(filepath + "_tmp");
Path originalPath = Paths.get(filepath);
// Files.copy(originalPath, copied, StandardCopyOption.REPLACE_EXISTING);
Files.copy(originalPath, copied1, StandardCopyOption.REPLACE_EXISTING);
Files.delete(Paths.get(filepath));
} catch (IOException ex) {
Logger.getLogger(CloneSheet.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void cloneSheets(String xlsfile, java.util.List<String> list,int copynum) {
ActiveXComponent app = new ActiveXComponent("Excel.Application");
try {
backup(xlsfile);
app.setProperty("Visible", new Variant(false));
Dispatch excels = app.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.invoke(
excels,
"Open",
Dispatch.Method,
new Object[]{xlsfile + "_tmp", new Variant(false),
new Variant(true)}, new int[1]).toDispatch();
//Dispatch sheets = Dispatch.get((Dispatch) excel, "Worksheets").toDispatch();
int sz = list.size();//"Angle_1pc_SBC_R1"
for (int i = 0; i < sz; i++) {
Dispatch sheet = Dispatch.invoke(excel, "Worksheets", Dispatch.Get,
new Object[]{list.get(i)}, new int[1]).toDispatch();//Whatever sheet you //wanted the new sheet inserted after
//Dispatch workbooksTest = app.getProperty("Sheets").toDispatch();//Get the workbook
//Dispatch sheet2 = Dispatch.call(workbooksTest, "Add").toDispatch();
for(int k=0;k<copynum -1;k++)
{
Dispatch.call(sheet, "Copy", sheet);
}
}
//Moves the sheet behind the desired sheet
Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[]{xlsfile, new Variant(52)}, new int[1]);
Variant f = new Variant(false);
Dispatch.call(excel, "Close", f);
Files.delete(Paths.get(xlsfile + "_tmp"));
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[]{});
}
}
/* public static void main(String args[])
{
java.util.ArrayList<String> list = new java.util.ArrayList();
list.add("Angle_1pc_SBC_R1");
new CloneSheet().cloneSheets("C:\LTDrive2_4\Excel\Test.xlsm", list, 2);
}*/
}
有类似的问题。
我有一个正在写入 excel 文件的线程。
通过将synchronized
添加到我用来写入excel
的函数中来解决它
我的代码中有以下方法 (createAdditionalSheetsInExcel
),它尝试为特定场景 (runOutputNumber > 1
) 创建额外的 sheet。它最终会创建一个 excel,但问题是当您尝试打开 excel 时,您最终会收到以下错误:
workBookObj.cloneSheet(index,sheetName)
在 java 代码中没有抛出任何错误,但是当我尝试打开 excel 文件时,出现以下错误:
我尝试删除 sheet 中 table 的格式,然后错误消失了。所以一定是跟sheet里面的table的格式有关。
private static void createAdditionalSheetsInExcel(String tempOutputFileName, String outputFileName, int runOutputNumber) throws IOException {
FileInputStream fileIn = new FileInputStream(tempOutputFileName);
XSSFWorkbook workBookObj = new XSSFWorkbook(fileIn);
workBookObj.setWorkbookType(XSSFWorkbookType.XLSM);
runOutputNumber = 2;//Hard coded for clarification
if (runOutputNumber > 1) {
int initialNoOfSheets = workBookObj.getNumberOfSheets();
for (int runIndex = 2; runIndex <= runOutputNumber; runIndex++) {
for (int index = 0; index < initialNoOfSheets; index++) {
XSSFSheet sheet = workBookObj.getSheetAt(index);
String sheetName = sheet.getSheetName().trim()
.substring(0, sheet.getSheetName().length() - 1) + runIndex;
workBookObj.cloneSheet(index, sheetName);
}
}
}
FileOutputStream fileOut = new FileOutputStream(outputFileName);
workBookObj.write(fileOut);
fileOut.close();
workBookObj.close();
deleteTempExcel(tempOutputFileName);
}
excel 尝试打开时出错:
我们发现 'abc.xlsm' 中的某些内容存在问题。你想尽可能多地恢复吗?如果您信任此工作簿的来源,请单击“是”。
错误:打开 excel 文件后:
已修复记录:Table 来自 /xl/tables/table1。xml 部分 (Table)
最终使用 jacob api 以及对模板进行更改解决了该问题。问题出在 Excel 中定义的局部变量,我可以通过转到(公式 -> 名称管理器)访问它并在删除变量后删除 variable.Even 我无法让它与 Apache 一起工作POI 因此最终使用了 Jacob api。代码如下:
package com.ford.ltdrive.model.output.excel.excelenum;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.Date;
import java.util.logging.Level;
import java.util.logging.Logger;
public class CloneSheet {
public void backup(String filepath) {
try {
Date d = new Date();
String dateString = (d.getYear() + 1900) + "_" + d.getMonth() + "_" + d.getDate();
// String backupfilepath = filepath.replace(".xlsm", "_backup_" + dateString + ".xlsm");
//Path copied = Paths.get(backupfilepath);
Path copied1 = Paths.get(filepath + "_tmp");
Path originalPath = Paths.get(filepath);
// Files.copy(originalPath, copied, StandardCopyOption.REPLACE_EXISTING);
Files.copy(originalPath, copied1, StandardCopyOption.REPLACE_EXISTING);
Files.delete(Paths.get(filepath));
} catch (IOException ex) {
Logger.getLogger(CloneSheet.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void cloneSheets(String xlsfile, java.util.List<String> list,int copynum) {
ActiveXComponent app = new ActiveXComponent("Excel.Application");
try {
backup(xlsfile);
app.setProperty("Visible", new Variant(false));
Dispatch excels = app.getProperty("Workbooks").toDispatch();
Dispatch excel = Dispatch.invoke(
excels,
"Open",
Dispatch.Method,
new Object[]{xlsfile + "_tmp", new Variant(false),
new Variant(true)}, new int[1]).toDispatch();
//Dispatch sheets = Dispatch.get((Dispatch) excel, "Worksheets").toDispatch();
int sz = list.size();//"Angle_1pc_SBC_R1"
for (int i = 0; i < sz; i++) {
Dispatch sheet = Dispatch.invoke(excel, "Worksheets", Dispatch.Get,
new Object[]{list.get(i)}, new int[1]).toDispatch();//Whatever sheet you //wanted the new sheet inserted after
//Dispatch workbooksTest = app.getProperty("Sheets").toDispatch();//Get the workbook
//Dispatch sheet2 = Dispatch.call(workbooksTest, "Add").toDispatch();
for(int k=0;k<copynum -1;k++)
{
Dispatch.call(sheet, "Copy", sheet);
}
}
//Moves the sheet behind the desired sheet
Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[]{xlsfile, new Variant(52)}, new int[1]);
Variant f = new Variant(false);
Dispatch.call(excel, "Close", f);
Files.delete(Paths.get(xlsfile + "_tmp"));
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[]{});
}
}
/* public static void main(String args[])
{
java.util.ArrayList<String> list = new java.util.ArrayList();
list.add("Angle_1pc_SBC_R1");
new CloneSheet().cloneSheets("C:\LTDrive2_4\Excel\Test.xlsm", list, 2);
}*/
}
有类似的问题。 我有一个正在写入 excel 文件的线程。
通过将synchronized
添加到我用来写入excel