【发布时间】:2013-09-05 06:52:43
【问题描述】:
在 C# 中打开或保存 Excel 时,我需要传递文件名 ref 而不是给出字符串(完整路径)。下面是一段代码sn-p
object fileName = (string)e.Argument;
object oMissing = System.Reflection.Missing.Value;
if (fileName.ToString().EndsWith("xlsx"))
{
Excel.Workbook wb;
object oMissing1 = Type.Missing;
var app = new Microsoft.Office.Interop.Excel.Application();
wb = app.Workbooks.Open(@"C:\Users\273714\Desktop\ProoferReport1.xlsx",
oMissing1, oMissing1, oMissing1, oMissing1,
oMissing1, oMissing1, oMissing1, oMissing1,
oMissing1, oMissing1, oMissing1, oMissing1,
oMissing1, oMissing1);
wb.SaveAs(@"C:\Users\273714\Desktop\Proofer Report2.xls",
Excel.XlFileFormat.xlExcel8, Type.Missing,
Type.Missing, Type.Missing, Type.Missing,
Excel.XlSaveAsAccessMode.xlExclusive,
Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
app.Quit();
app.Quit();
}
如果是word文档,我可以直接打开它
oDoc = oWord.Documents.Open(ref fileName, ref oMissing,
ref readOnly, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref oMissing, ref oMissing, ref oMissing,
ref oMissing, ref isVisible, ref oMissing, ref oMissing,
ref oMissing, ref oMissing);
我也需要在 Excel 中使用相同的文件名方式。任何帮助将不胜感激。
【问题讨论】:
-
不确定您的问题究竟是什么意思。您想使用传递
filename.xlsx而不是C:\filename.xlsx? -
我想在浏览文件并打开它时传递对象文件名而不是字符串。文件名不断变化。我不想硬编码它
-
你有
fileName对象中文件的完整路径吗? -
所以只需使用文件打开对话框,让用户选择文件,检查文件是否正确,然后显示另存为对话框供用户再次保存.....
-
我很困惑..你为什么不能只使用
fileName变量? (或只是File.Copy)