嘿,我的代码问题,我真的不想工作,一直在使用Word互操作,但当我将其更改为Excel时,它一直在私有虚拟CreateWordDocument(Excel.document Adoc和excelapp.document)中返回错误信息。开)
private void FindAndReplace(Microsoft.Office.Interop.Excel.Application wordApp, object findText, object replaceWithText)
{
object matchCase = true;
object matchWholeWord = true;
object matchWildCards = false;
object matchSoundLike = false;
object nmatchAllForms = false;
object forward = true;
object format = false;
object matchKashida = false;
object matchDiactitics = false;
object matchAlefHamza = false;
object matchControl = false;
object read_only = false;
object visible = true;
object replace = 2;
object wrap = 1;
wordApp.Selection.Find.Execute(ref findText,
ref matchCase, ref matchWholeWord,
ref matchWildCards, ref matchSoundLike,
ref nmatchAllForms, ref forward,
ref wrap, ref format, ref replaceWithText,
ref replace, ref matchKashida,
ref matchDiactitics, ref matchAlefHamza,
ref matchControl);
}
string pathImage = null;
private void CreateWordDocument(object filename, object savaAs, object image)
{
List<int> processesbeforegen = getRunningProcesses();
object missing = Missing.Value;
Excel.Application excelApp = new Excel.Application();
Excel.Document aDoc = null;
if (File.Exists((string)filename))
{
DateTime today = DateTime.Now;
object readOnly = false; //default
object isVisible = false;
excelApp.Visible = false;
aDoc = excelApp.Document.Open(ref filename, ref missing, ref readOnly,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing);
aDoc.Activate();
//Find and replace:
this.FindAndReplace(excelApp, "$$firstname$$", txtFirstName.Text);
}
else
{
MessageBox.Show("file dose not exist.");
return;
}
//Save as: filename
aDoc.SaveAs2(ref savaAs, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
//Close Document:
//aDoc.Close(ref missing, ref missing, ref missing);
MessageBox.Show("File created.");
List<int> processesaftergen = getRunningProcesses();
killProcesses(processesbeforegen, processesaftergen);
}
public List<int> getRunningProcesses()
{
List<int> ProcessIDs = new List<int>();
//here we're going to get a list of all running processes on
//the computer
foreach (Process clsProcess in Process.GetProcesses())
{
if (Process.GetCurrentProcess().Id == clsProcess.Id)
continue;
if (clsProcess.ProcessName.Contains("WINEXCEL"))
{
ProcessIDs.Add(clsProcess.Id);
}
}
return ProcessIDs;
}
private void killProcesses(List<int> processesbeforegen, List<int> processesaftergen)
{
foreach (int pidafter in processesaftergen)
{
bool processfound = false;
foreach (int pidbefore in processesbeforegen)
{
if (pidafter == pidbefore)
{
processfound = true;
}
}
if (processfound == false)
{
Process clsProcess = Process.GetProcessById(pidafter);
clsProcess.Kill();
}
}
}
//Méthode Enabled Controles:
private void tEnabled(bool state)
{
txtFirstName.Enabled = state;
}
//Load the Template Document:
private void button1_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
tFilename.Text = openFileDialog1.FileName;
tEnabled(true);
}
}
private void button2_Click(object sender, EventArgs e)
{
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
CreateWordDocument(tFilename.Text, saveFileDialog1.FileName, pathImage);
tEnabled(false);
//printDocument1.DocumentName = SaveDoc.FileName;
}
}
}
}
答案 0 :(得分:0)
Word Interop和Excel Interop非常不同。您不能只将“Word”更改为“Excel”。您可能必须选择工作表,定义一系列单元格,循环遍历范围等等... Google上有大量的片段。