问题: 用户向文件夹发送了多个.xls文件。 我们使用的数据处理应用程序无法正确读取它。 但是,当我们打开excel表并将其保存回来时,它可以工作。 所以我们决定编写一个程序来自动完成。
编写的VB脚本基本上是打开excel文件,创建一个新的空文件并将每个工作表复制到新文件。我们遇到的问题是来自Office的“兼容性提示”将始终在保存新文件时弹出。
我正在尝试创建一个C#Windows窗体应用程序,它也会这样做。我在stackoverflow中经历了很多关于这个问题的帖子但是我无法使它工作。目前我正在测试的代码是
Microsoft.Office.Interop.Excel.ApplicationClass app = new ApplicationClass();
Workbook curWorkBook = null;
Workbook destWorkbook = null;
Worksheet workSheet = null;
Worksheet newWorksheet = null;
Object defaultArg = Type.Missing;
try
{
Copy the source sheet
curWorkBook = app.Workbooks.Open("d:\\oldexcel.xls", defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg);
workSheet = (Worksheet)curWorkBook.Sheets[1];
workSheet.UsedRange.Copy(defaultArg);
Paste on destination sheet
destWorkbook = app.Workbooks.Open("d:\\oldexcel2.xls", defaultArg, false, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg, defaultArg);
newWorksheet = (Worksheet)destWorkbook.Worksheets.Add(defaultArg, defaultArg, defaultArg, defaultArg);
newWorksheet.UsedRange._PasteSpecial(XlPasteType.xlPasteValues, XlPasteSpecialOperation.xlPasteSpecialOperationNone, false, false);
}
catch (Exception exc)
{
System.Windows.Forms.MessageBox.Show(exc.Message);
}
finally
{
if (curWorkBook != null)
{
curWorkBook.Save();
curWorkBook.Close(defaultArg, defaultArg, defaultArg);
}
if (destWorkbook != null)
{
destWorkbook.Save();
destWorkbook.Close(defaultArg, defaultArg, defaultArg);
}
}
app.Quit();
这根本不起作用,并且会创建一个损坏的文件。
下面是我写的VB脚本。
Dim objExcel, objWorkbook, wbSrc
Dim strFileName, strDirectory, extension, Filename, strFileName2
Dim objFSO, objFolder, objFile
strFileName = "C:\Users\ThirteenCoin\Desktop\testcopy\WH performance KPI project plan 20152.xlsx" '--source file
strFileName2 = "C:\Users\ThirteenCoin\Desktop\testcopy\testcopy\WH performance KPI project plan 20152.xlsx" '--tgtfile
Set objExcel = CreateObject("Excel.Application")
objExcel.Visible = True
Set objWorkbook = objExcel.Workbooks.Add()
extension = "xlsx"
strDirectory = "C:\Users\ThirteenCoin\Desktop\testcopy" '--source directory
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(strDirectory)
For Each objFile In objFolder.Files
If LCase((objFSO.GetExtensionName(objFile))) = LCase(extension) Then
Filename = objFile.Name
Filename = strDirectory & "\" & Filename
Set wbSrc = objExcel.Workbooks.Open(Filename)
wbSrc.Sheets(1).Copy objWorkbook.Sheets(objWorkbook.Sheets.Count)
wbSrc.Close
End If
Next
'~~> Close and Cleanup
objWorkbook.SaveAs (strFileName2)
objWorkbook.Close
objExcel.Quit
Set wbSrc = Nothing
Set objWorkbook = Nothing
Set objExcel = Nothing
请告知如何避免兼容性提示,或者是否存在针对这种奇怪要求的更好解决方案。
由于