根据值从表中创建组/块

时间:2018-11-14 14:26:33

标签: c# sql-server linq ms-access excel-interop

我在datagridview中有input table(输出显示为绿色),我需要获得以下输出:

'Start of block'   'Size'  'TypKar'
 1.2.2017 0:00:02   14      6280
 1.2.2017 0:03:33    2      3147
 1.2.2017 0:04:17    2      4147
 1.2.2017 0:04:28    2      6280
 1.2.2017 0:04:59   10      3147

现在,我使用for循环,在该循环中我写第一个条目,然后计数,直到TypKar列中的值更改为止。当它改变时,我写上日期和类型,然后从1开始计数。

for(int i = 0; i < dviewExport.RowCount; i++)
{
    //first line in excel
    if(totalCount == 0) 
    {
        totalCount = 32; 
        signCount = 1;
        excelWsExport.Cells[totalCount, 2] = (DateTime)dviewExport[0, i].Value;
        excelWsExport.Cells[totalCount, 3] = 1;
        excelWsExport.Cells[totalCount, 4] = dviewExport["TypKar", i].Value;
        continue;
    }
    //value is same = just increment
    if((excelWsExport.Cells[totalCount, 4] as Excel.Range).Value.ToString() == dviewExport["TypKar", i].Value.ToString())
    {
        excelWsExport.Cells[totalCount, 3] = (excelWsExport.Cells[totalCount, 3] as Excel.Range).Value + 1;
        signCount++;
        if(maxCount < signCount)
            maxCount = signCount;
    }
    //value changed = write new line and restart incrementing
    else
    {
        totalCount++;
        signCount = 1;
        excelWsExport.Cells[totalCount, 2] = (DateTime)dviewExport[0, i].Value;
        excelWsExport.Cells[totalCount, 3] = 1;
        excelWsExport.Cells[totalCount, 4] = dviewExport["TypKar", i].Value;
    }
}

问题是,我将其编写为excel,并且当数据具有数千行时,这需要大量时间。

是否可以使用 excel interop 加快速度-将其写入数组,然后将数组粘贴到excel / sql / ling 还有什么吗?

我试图找到类似的问题并得到一些答案,但是我不知道如何描述我的问题。

2 个答案:

答案 0 :(得分:0)

在我目前正在处理的一个应用程序中,我使用类似于以下内容的东西:

string connectionString = "my connection string";



            for (int i = 0; i < dataGridView1.RowCount - 1; i++)
            {
                DataGridViewRow row = dataGridView1.Rows[i];

                SqlConnection conn = new SqlConnection(connectionString);
                conn.Open();

                try
                {

                    var queryString = "INSERT INTO [SQLdb] " +
                        "(columnNamesInDB) " +
                        "VALUES (@dataBeingRead)";
                    SqlCommand comm = new SqlCommand(queryString, conn);
                    comm.ExecuteNonQuery();
                    comm.Close();
                  }
catch (Exception e)
{
//catch behavior
}

遍历网格视图中的每个值并插入到SQL Server中。就我们的目的而言,工作起来非常快(目前大约有1000个范围)。

答案 1 :(得分:0)

基于Export a C# List of Lists to Excel,我设法通过创建通用列表来加快处理速度,然后将其粘贴到具有二维的对象列表中,然后将这些创建的列表扩展到excel范围。这比每次写入excel单元的速度都要快。

问题是Excel不喜欢List<T>list[]都不喜欢。您必须发送到excel object[,](二维),由于我只有一个维度,因此我做了第二个1维。

//create generic lists
List<DateTime> listDate = new List<DateTime>();                                     
List<int> listSize = new List<int>();                                       
List<string> listSign = new List<string>();                                     

//fill lists with data from wherever
for(int i = 0; i < dviewExport.RowCount; i++)                                       
{                                       
    if(listSign.Count == 0)                             
    {                               
        signCount = 1;                          
        listDate.Add((DateTime)dviewExport[0, i].Value);                            
        listSize.Add(1);                            
        listSign.Add((string)dviewExport[$"{Sign}", i].Value);                      
        continue;                           
    }                               
    if(listSign[listSign.Count - 1] == dviewExport[$"{Sign}", i].Value.ToString())                              
    {                               
        listSize[listSize.Count - 1] += 1;                          
        signCount++;                            
        if(maxCount < signCount)                            
            maxCount = signCount;                       
    }                               
    else                                
    {                               
        signCount = 1;                          
        listDate.Add((DateTime)dviewExport[0, i].Value);                            
        listSize.Add(1);                            
        listSign.Add((string)dviewExport[$"{Sign}", i].Value);                          
    }                               
}       

//create two dimensional object lists with size of generic lists
object[,] outDate = new object[listDate.Count, 1];                                      
object[,] outSize = new object[listSize.Count, 1];                                      
object[,] outSign = new object[listSign.Count, 1];                                      

//fill two dimensional object lists with data from generic lists
for(int row = 0; row < listDate.Count; row++)
{
    outDate[row, 0] = listDate[row];                                    
    outSize[row, 0] = listSize[row];                                    
    outSign[row, 0] = listSign[row];
}                           

//set Excel ranges and paste lists
range = excelWsExport.get_Range($"B32:B{32 + listDate.Count}", Type.Missing);                                       
range.NumberFormat = "d.MM.yyyy H:mm:ss";                                       
range.Value = outDate;                                      

range = excelWsExport.get_Range($"C32:C{32 + listSize.Count}", Type.Missing);                                       
range.Value = outSize;                                      

range = excelWsExport.get_Range($"D32:D{32 + listSign.Count}", Type.Missing);                                       
range.Value = outSign;