如何比较当前生成的excel和以前生成的excel以删除excel interop中的所有匹配行?

问题描述

我已经在数据库表中创建了一条触发语句,数据将存储在新的物理表中。现在,我创建了一个控制台应用程序,使用excel interop将数据从物理表提取到excel表中。

每次运行该应用程序时,我只希望未导出的数据显示在新的Excel中,而不显示所有内容。就像我想与以前生成的excel比较并删除当前生成的excel中已经存在的数据一样。

例如: Stock.xls数据: 一种 乙 C

数据库表数据: 一种 乙 C

当我第二次运行该应用程序时(我在db的物理表中添加了新行,因此新的xl工作表应删除a,b,c(并且仅必须显示d)

Stock.xls数据: D

数据库表数据: 一种 乙 C D

这是我的代码:

string connectionstring = System.Configuration.ConfigurationManager.ConnectionStrings["IntegrationConnection"].ConnectionString;
string sql2 = null;
string data2 = null;
int k = 0;
int l = 0;
string Filename2 = @"D:\Integration\Stock.xls";
if (!File.Exists(Filename2))
{
    File.Create(Filename2).Dispose();
    using (TextWriter tw = new StreamWriter(Filename2))
    {
        tw.WriteLine("Please run the program again");
        tw.Close();
    }
}

////*** Preparing excel Application
Excel.Application xlApp2;
Excel.Workbook xlWorkBook2;
Excel.Worksheet xlWorkSheet2;
object misValue2 = System.Reflection.Missing.Value;

///*** Opening Excel application
xlApp2 = new Microsoft.Office.Interop.Excel.Application();
xlWorkBook2 = xlApp2.Workbooks.Open(Filename2);
xlWorkSheet2 = (Excel.Worksheet)(xlWorkBook2.ActiveSheet as Excel.Worksheet);

xlApp2.DisplayAlerts = false;
SqlConnection conn2 = new SqlConnection(connectionstring);
conn2.Open();

sql2 = "SELECT * from tblMPartHistory";

///*** Preparing to retrieve value from the database
DataTable dtable2 = new DataTable();

SqlDataAdapter dscmd2 = new SqlDataAdapter(sql2,conn2);
DataSet ds2 = new DataSet();
dscmd2.Fill(dtable2);

////*** Generating the column Names here
string[] colNames2 = new string[dtable2.Columns.Count];
int col2 = 0;

foreach (DataColumn dc in dtable2.Columns)
    colNames2[col2++] = dc.ColumnName;

char lastColumn2 = (char)(51 + dtable2.Columns.Count - 1);

xlWorkSheet2.get_Range("A1",lastColumn2 + "1").Value2 = colNames2;
xlWorkSheet2.get_Range("A1",lastColumn2 + "1").Font.Bold = true;

xlWorkSheet2.get_Range("A1",lastColumn2 + "1").VerticalAlignment
                   = Excel.XlVAlign.xlVAlignCenter;

/////*** Inserting the Column and Values into Excel file
for (k = 0; k <= dtable2.Rows.Count - 1; k++)
{
    for (l = 0; l <= dtable2.Columns.Count - 1; l++)
    {
        data2 = dtable2.Rows[k].ItemArray[l].ToString();
        xlWorkSheet2.Cells[k + 2,l + 1] = data2;
        xlWorkBook2.Save();
    }
}

xlWorkBook2.Close(true,misValue2,misValue2);
xlApp2.Quit();

System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkSheet2);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlWorkBook2);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp2);

解决方法

这就是我对您的问题的理解:

您有一个Database表,并且您希望每次将新行“提交”到数据库时都生成一个新的excel工作表,仅反映最近添加的数据。

当然,您可以采用这种方法,只需将数据与所有最近的Excel工作表进行比较,以删除重复项。

一种更好的方法是将时间戳记或会话ID添加到数据集中。 然后,您可以查询数据库,并从所有具有最新时间戳或最高会话ID的行中生成新表。

通过这种方式,您不仅可以省去重复副本的额外工作,而且还可以在丢失所有工作表的情况下恢复所有工作表。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...