使用Excel COM lib将文件保存回SharePoint时出现问题。 我打开文件,但它打开为“xlviewer.xlsx”,所以我假设我需要覆盖原始文件。 (我删除了try / catch以进行一些错误处理。)当我这样做时,我收到此错误:“索引超出列表末尾”(行错误:42'SaveAs statement')
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.IO;
namespace CopyInteractionIds
{
class Program
{
static void Main(string[] args)
{
Excel.Application oApp;
Excel._Workbook oWbFrom;
Excel._Workbook oWbTo;
Excel._Worksheet oWsFrom;
Excel._Worksheet oWsTo;
Excel.Range oRngFrom;
Excel.Range oRngTo;
//try
//{
oApp = new Excel.Application();
oApp.Visible = true;
Console.WriteLine("Opening source workbook");
oWbFrom = (Excel._Workbook)(oApp.Workbooks.Open(@"path to my source wb"
, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value));
Console.WriteLine("Opening edited workbook");
oWbTo = (Excel._Workbook)(oApp.Workbooks.Open(@"https://path to my out wb"
, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value
, Missing.Value, true, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value));
Console.WriteLine("Copying data");
oWsFrom = (Excel._Worksheet)oWbFrom.Worksheets.get_Item("Interaction Number Lookup");
oWsTo = (Excel._Worksheet)oWbTo.Worksheets.get_Item("Interaction Number Lookup");
oRngFrom = oWsFrom.get_Range("=OFFSET($A$1,0,0,COUNTA($A:$A),COUNTA($1:$1))",Missing.Value);
oWsTo.get_Range("=OFFSET($A$1,0,0,COUNTA($A:$A),COUNTA($1:$1))", Missing.Value).Clear();
oRngTo = oWsTo.get_Range("A1",Missing.Value);
oRngFrom.Copy(oRngTo);
Console.WriteLine("Saving edited file");
oWbTo.SaveAs(@"path to out file"
, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value
,Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, Missing.Value
, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
oWbTo.Close(false,Missing.Value,Missing.Value);
oWbFrom.Close(false, Missing.Value, Missing.Value);
oApp.Quit();
//}
//catch(Exception ex)
//{
// Console.WriteLine("Unable to process job: {0}",ex.Message);
//}
}
}
}
答案 0 :(得分:1)
我遇到了类似的问题,原来我的文件被“损坏”了。打开它们,让Excel修复它们然后覆盖损坏的文件似乎对我有用。
答案 1 :(得分:0)
其他人打开了文件。