通过 Microsoft Interop c# 附加到现有 excel 文件
append on existing excel file via microsoft Interop c#
伙计们!我正在编写一个程序,通过 selenium 网络驱动程序从网站获取数据。我正在尝试为我们的项目创建足球装置。到目前为止,我完成了从网站上获取日期和时间、球队名称和比分。一两天前,我问有没有办法将数据写入excel文件。我试过epplus,但是不好用,所以我开始用Interop,我有几个问题。
首先,我需要检查我的文件是否存在。另外,我需要访问当前工作表。然后,我需要检查第一行项目是否存在。如果它不存在,我需要在索引 1 中插入一个新行。我不确定我做对了:
using Excel = Microsoft.Office.Interop.Excel; //namespace
var filePath = new FileInfo(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) +
"\Test" + "\" + pathName + "\" + subFile + "\" + pathName + ".xlsx");
Excel.Application xlApp = new Excel.Application();
xlApp.Visible = true;
object misValue = System.Reflection.Missing.Value;
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\Test" + "\" + pathName + "\" + subFile + "\" + pathName + ".xlsx";
if (filePath.Exists)
{
Excel.Workbook wb = xlApp.Workbooks.Open(path,0, false, misValue, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets.Item["Sheet1"];
for (int i = 0; i < dateTime.Count; i++)
{
if (string.Compare(ws.Cells[i + 1, 1], dateTime[dateTime.Count - i -1]) != 0 && string.Compare(ws.Cells[i + 1, 2], firstTeam[dateTime.Count - i - 1]) != 0 && string.Compare(ws.Cells[i + 1, 3], secondTeam[dateTime.Count - i - 1]) != 0)
{
ws.Cells.Rows.Insert(0);
ws.Cells[1, 1] = dateTime[dateTime.Count - i - 1];
ws.Cells[1, 2] = firstTeam[dateTime.Count - i - 1];
ws.Cells[1, 3] = secondTeam[dateTime.Count - i - 1];
}
}
}
我正在尝试检查行中的数据,但无法完成。我的 if 条件不起作用 我收到此错误:best overloaded method match for 'string.Compare(string, string)' has some invalid arguments'
我该怎么做?另外,我的代码正确吗?
String.Compare()有一些重载的方法,你可以在这里查看:https://msdn.microsoft.com/en-us//library/system.string.compare(v=vs.110).aspx
因此,您应该在其中传递两个字符串,但您传递了 Cells 和 dateTime、Cells 和 firstTeam 以及 Cells 和 secondTeam。所以,你需要把它们带到相同类型的System.String。我更喜欢 String.Equals() (https://msdn.microsoft.com/en-us//library/1hkt4325(v=vs.110).aspx) 而不是 String.Compare(),因为 String.Equals() return 布尔值,所以不需要比较它与 0。假设我不知道 dateTime、fisrtTeam 和 secondTeam 对象的类型,例如,您的 if 条件可能如下所示:
if (string.Equals(ws.Cells[i + 1, 1].Value.ToString(), dateTime[dateTime.Count - i -1].ToString()) && string.Equals(ws.Cells[i + 1, 2].Value.ToString(), firstTeam[dateTime.Count - i - 1].ToString()) && string.Equals(ws.Cells[i + 1, 3].Value.ToString(), secondTeam[dateTime.Count - i - 1]).ToString())
当然,你应该避免写这么长的条件,因为你很容易错过一些东西。
此外:在 if body 内部,您将 Cells 分配给 dateTime、firstTeam 和 secondTeam,它们可能是不同的类型。所以,它可能会失败。
好吧,首先,我在使用header时看不到.Value
using Excel = Microsoft.Office.Interop.Excel;
所以,我想,没有任何用途。然而,当我尝试使用它时,它没有发生任何错误,我的代码运行完美。
更新版本:
using Excel = Microsoft.Office.Interop.Excel; //namespace
var filePath = new
FileInfo(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) +
"\Test" + "\" + pathName + "\" + subFile + "\" + pathName + ".xlsx");
Excel.Application xlApp = new Excel.Application();
xlApp.Visible = true;
object misValue = System.Reflection.Missing.Value;
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\Test" + "\" + pathName + "\" + subFile + "\" + pathName + ".xlsx";
if (filePath.Exists) // checks whether file exist or not
{
Excel.Workbook wb = xlApp.Workbooks.Open(path,0, false, misValue, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets.Item["Sheet1"];
for (int i = 0; i < dateTime.Count; i++)
{
// check element already there or not
if (ws.Cells[i + 1, 1].Value.ToString() != dateTime[dateTime.Count - i - 1])
{
ws.Rows["1"].insert(); // add new row for new data
ws.Cells[1, 1] = dateTime[dateTime.Count - i - 1];
ws.Cells[1, 2] = firstTeam[dateTime.Count - i - 1];
ws.Cells[1, 3] = secondTeam[dateTime.Count - i - 1];
}
}
wb.SaveAs(path, Excel.XlFileFormat.xlOpenXMLWorkbook, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlShared, misValue, misValue, misValue, misValue, misValue);
wb.Close(true, misValue, misValue);
xlApp.Quit();
}
伙计们!我正在编写一个程序,通过 selenium 网络驱动程序从网站获取数据。我正在尝试为我们的项目创建足球装置。到目前为止,我完成了从网站上获取日期和时间、球队名称和比分。一两天前,我问有没有办法将数据写入excel文件。我试过epplus,但是不好用,所以我开始用Interop,我有几个问题。
首先,我需要检查我的文件是否存在。另外,我需要访问当前工作表。然后,我需要检查第一行项目是否存在。如果它不存在,我需要在索引 1 中插入一个新行。我不确定我做对了:
using Excel = Microsoft.Office.Interop.Excel; //namespace
var filePath = new FileInfo(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) +
"\Test" + "\" + pathName + "\" + subFile + "\" + pathName + ".xlsx");
Excel.Application xlApp = new Excel.Application();
xlApp.Visible = true;
object misValue = System.Reflection.Missing.Value;
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\Test" + "\" + pathName + "\" + subFile + "\" + pathName + ".xlsx";
if (filePath.Exists)
{
Excel.Workbook wb = xlApp.Workbooks.Open(path,0, false, misValue, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets.Item["Sheet1"];
for (int i = 0; i < dateTime.Count; i++)
{
if (string.Compare(ws.Cells[i + 1, 1], dateTime[dateTime.Count - i -1]) != 0 && string.Compare(ws.Cells[i + 1, 2], firstTeam[dateTime.Count - i - 1]) != 0 && string.Compare(ws.Cells[i + 1, 3], secondTeam[dateTime.Count - i - 1]) != 0)
{
ws.Cells.Rows.Insert(0);
ws.Cells[1, 1] = dateTime[dateTime.Count - i - 1];
ws.Cells[1, 2] = firstTeam[dateTime.Count - i - 1];
ws.Cells[1, 3] = secondTeam[dateTime.Count - i - 1];
}
}
}
我正在尝试检查行中的数据,但无法完成。我的 if 条件不起作用 我收到此错误:best overloaded method match for 'string.Compare(string, string)' has some invalid arguments'
我该怎么做?另外,我的代码正确吗?
String.Compare()有一些重载的方法,你可以在这里查看:https://msdn.microsoft.com/en-us//library/system.string.compare(v=vs.110).aspx
因此,您应该在其中传递两个字符串,但您传递了 Cells 和 dateTime、Cells 和 firstTeam 以及 Cells 和 secondTeam。所以,你需要把它们带到相同类型的System.String。我更喜欢 String.Equals() (https://msdn.microsoft.com/en-us//library/1hkt4325(v=vs.110).aspx) 而不是 String.Compare(),因为 String.Equals() return 布尔值,所以不需要比较它与 0。假设我不知道 dateTime、fisrtTeam 和 secondTeam 对象的类型,例如,您的 if 条件可能如下所示:
if (string.Equals(ws.Cells[i + 1, 1].Value.ToString(), dateTime[dateTime.Count - i -1].ToString()) && string.Equals(ws.Cells[i + 1, 2].Value.ToString(), firstTeam[dateTime.Count - i - 1].ToString()) && string.Equals(ws.Cells[i + 1, 3].Value.ToString(), secondTeam[dateTime.Count - i - 1]).ToString())
当然,你应该避免写这么长的条件,因为你很容易错过一些东西。
此外:在 if body 内部,您将 Cells 分配给 dateTime、firstTeam 和 secondTeam,它们可能是不同的类型。所以,它可能会失败。
好吧,首先,我在使用header时看不到.Value
using Excel = Microsoft.Office.Interop.Excel;
所以,我想,没有任何用途。然而,当我尝试使用它时,它没有发生任何错误,我的代码运行完美。
更新版本:
using Excel = Microsoft.Office.Interop.Excel; //namespace
var filePath = new
FileInfo(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) +
"\Test" + "\" + pathName + "\" + subFile + "\" + pathName + ".xlsx");
Excel.Application xlApp = new Excel.Application();
xlApp.Visible = true;
object misValue = System.Reflection.Missing.Value;
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\Test" + "\" + pathName + "\" + subFile + "\" + pathName + ".xlsx";
if (filePath.Exists) // checks whether file exist or not
{
Excel.Workbook wb = xlApp.Workbooks.Open(path,0, false, misValue, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
Excel.Worksheet ws = (Excel.Worksheet)wb.Worksheets.Item["Sheet1"];
for (int i = 0; i < dateTime.Count; i++)
{
// check element already there or not
if (ws.Cells[i + 1, 1].Value.ToString() != dateTime[dateTime.Count - i - 1])
{
ws.Rows["1"].insert(); // add new row for new data
ws.Cells[1, 1] = dateTime[dateTime.Count - i - 1];
ws.Cells[1, 2] = firstTeam[dateTime.Count - i - 1];
ws.Cells[1, 3] = secondTeam[dateTime.Count - i - 1];
}
}
wb.SaveAs(path, Excel.XlFileFormat.xlOpenXMLWorkbook, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlShared, misValue, misValue, misValue, misValue, misValue);
wb.Close(true, misValue, misValue);
xlApp.Quit();
}