Creating groups/blocks from table based on value
I have input table in datagridview (output is showed in green) and I need to get to this output:
'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
Right now I use for
loop in which I write first entry and then I count until value in column TypKar changes. When it changes, I write date and type and start counting from 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;
}
}
Problem is, that I write it to excel and when data have several thousands of rows it takes a lot of time.
Is it possible to speed it up with excel interop - write it to array and then paste array to excel / sql / ling or anything else?
I tried to find similar problem and get some answers but I don't know how to describe my problem.
c# sql-server linq ms-access excel-interop
add a comment |
I have input table in datagridview (output is showed in green) and I need to get to this output:
'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
Right now I use for
loop in which I write first entry and then I count until value in column TypKar changes. When it changes, I write date and type and start counting from 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;
}
}
Problem is, that I write it to excel and when data have several thousands of rows it takes a lot of time.
Is it possible to speed it up with excel interop - write it to array and then paste array to excel / sql / ling or anything else?
I tried to find similar problem and get some answers but I don't know how to describe my problem.
c# sql-server linq ms-access excel-interop
have you thought about using aParallel.For
loop, I did something similar recently and reduced my run-time from 35 min to 5 minutes docs.microsoft.com/en-us/dotnet/standard/parallel-programming/…. it doesn't seem like the order matters so this should work for you
– Andrei
Nov 14 '18 at 14:31
How is this related to Microsoft Access?
– Erik A
Nov 14 '18 at 14:42
Excel Interop can only slow things down by at least an order of magnitude, as each call is a cross-process call. If you want to generate an Excel file you don't need a DataGridView either. You can use a library like EPPlus to create real Excel files without using Excel very easily. For example, loading a DataTable into a sheet is as easy assheet.Cells.LoadFromDataTable(dataTable);
– Panagiotis Kanavos
Nov 14 '18 at 14:42
@Andrei this won't help at all. You can't access the same Excel instance from multiple clients/threads like this
– Panagiotis Kanavos
Nov 14 '18 at 14:43
Data I load to dgv are either from Access db or SQL Server. Also I have to send it to excel, because of next work afterwards
– Stana Macala
Nov 14 '18 at 14:58
add a comment |
I have input table in datagridview (output is showed in green) and I need to get to this output:
'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
Right now I use for
loop in which I write first entry and then I count until value in column TypKar changes. When it changes, I write date and type and start counting from 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;
}
}
Problem is, that I write it to excel and when data have several thousands of rows it takes a lot of time.
Is it possible to speed it up with excel interop - write it to array and then paste array to excel / sql / ling or anything else?
I tried to find similar problem and get some answers but I don't know how to describe my problem.
c# sql-server linq ms-access excel-interop
I have input table in datagridview (output is showed in green) and I need to get to this output:
'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
Right now I use for
loop in which I write first entry and then I count until value in column TypKar changes. When it changes, I write date and type and start counting from 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;
}
}
Problem is, that I write it to excel and when data have several thousands of rows it takes a lot of time.
Is it possible to speed it up with excel interop - write it to array and then paste array to excel / sql / ling or anything else?
I tried to find similar problem and get some answers but I don't know how to describe my problem.
c# sql-server linq ms-access excel-interop
c# sql-server linq ms-access excel-interop
asked Nov 14 '18 at 14:26
Stana MacalaStana Macala
85
85
have you thought about using aParallel.For
loop, I did something similar recently and reduced my run-time from 35 min to 5 minutes docs.microsoft.com/en-us/dotnet/standard/parallel-programming/…. it doesn't seem like the order matters so this should work for you
– Andrei
Nov 14 '18 at 14:31
How is this related to Microsoft Access?
– Erik A
Nov 14 '18 at 14:42
Excel Interop can only slow things down by at least an order of magnitude, as each call is a cross-process call. If you want to generate an Excel file you don't need a DataGridView either. You can use a library like EPPlus to create real Excel files without using Excel very easily. For example, loading a DataTable into a sheet is as easy assheet.Cells.LoadFromDataTable(dataTable);
– Panagiotis Kanavos
Nov 14 '18 at 14:42
@Andrei this won't help at all. You can't access the same Excel instance from multiple clients/threads like this
– Panagiotis Kanavos
Nov 14 '18 at 14:43
Data I load to dgv are either from Access db or SQL Server. Also I have to send it to excel, because of next work afterwards
– Stana Macala
Nov 14 '18 at 14:58
add a comment |
have you thought about using aParallel.For
loop, I did something similar recently and reduced my run-time from 35 min to 5 minutes docs.microsoft.com/en-us/dotnet/standard/parallel-programming/…. it doesn't seem like the order matters so this should work for you
– Andrei
Nov 14 '18 at 14:31
How is this related to Microsoft Access?
– Erik A
Nov 14 '18 at 14:42
Excel Interop can only slow things down by at least an order of magnitude, as each call is a cross-process call. If you want to generate an Excel file you don't need a DataGridView either. You can use a library like EPPlus to create real Excel files without using Excel very easily. For example, loading a DataTable into a sheet is as easy assheet.Cells.LoadFromDataTable(dataTable);
– Panagiotis Kanavos
Nov 14 '18 at 14:42
@Andrei this won't help at all. You can't access the same Excel instance from multiple clients/threads like this
– Panagiotis Kanavos
Nov 14 '18 at 14:43
Data I load to dgv are either from Access db or SQL Server. Also I have to send it to excel, because of next work afterwards
– Stana Macala
Nov 14 '18 at 14:58
have you thought about using a
Parallel.For
loop, I did something similar recently and reduced my run-time from 35 min to 5 minutes docs.microsoft.com/en-us/dotnet/standard/parallel-programming/…. it doesn't seem like the order matters so this should work for you– Andrei
Nov 14 '18 at 14:31
have you thought about using a
Parallel.For
loop, I did something similar recently and reduced my run-time from 35 min to 5 minutes docs.microsoft.com/en-us/dotnet/standard/parallel-programming/…. it doesn't seem like the order matters so this should work for you– Andrei
Nov 14 '18 at 14:31
How is this related to Microsoft Access?
– Erik A
Nov 14 '18 at 14:42
How is this related to Microsoft Access?
– Erik A
Nov 14 '18 at 14:42
Excel Interop can only slow things down by at least an order of magnitude, as each call is a cross-process call. If you want to generate an Excel file you don't need a DataGridView either. You can use a library like EPPlus to create real Excel files without using Excel very easily. For example, loading a DataTable into a sheet is as easy as
sheet.Cells.LoadFromDataTable(dataTable);
– Panagiotis Kanavos
Nov 14 '18 at 14:42
Excel Interop can only slow things down by at least an order of magnitude, as each call is a cross-process call. If you want to generate an Excel file you don't need a DataGridView either. You can use a library like EPPlus to create real Excel files without using Excel very easily. For example, loading a DataTable into a sheet is as easy as
sheet.Cells.LoadFromDataTable(dataTable);
– Panagiotis Kanavos
Nov 14 '18 at 14:42
@Andrei this won't help at all. You can't access the same Excel instance from multiple clients/threads like this
– Panagiotis Kanavos
Nov 14 '18 at 14:43
@Andrei this won't help at all. You can't access the same Excel instance from multiple clients/threads like this
– Panagiotis Kanavos
Nov 14 '18 at 14:43
Data I load to dgv are either from Access db or SQL Server. Also I have to send it to excel, because of next work afterwards
– Stana Macala
Nov 14 '18 at 14:58
Data I load to dgv are either from Access db or SQL Server. Also I have to send it to excel, because of next work afterwards
– Stana Macala
Nov 14 '18 at 14:58
add a comment |
2 Answers
2
active
oldest
votes
In one of the applications I'm working on right now I use something similar to:
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
}
To loop through every value in the grid view and insert into an SQL server. Works pretty quickly for our purposes (~1000 range currently).
The question is not about storing to the database.
– Panagiotis Kanavos
Nov 14 '18 at 14:44
add a comment |
Based on Export a C# List of Lists to Excel I managed to fast things up by creating generic lists, then pasting it to object lists with two dimensions and then these created lists to excel range. This is way more faster than writing each time to excel cell.
Problem is that Excel does not like List<T>
or either list
. You have to send to excel object[,]
(two dimensional) and since I had just one dimension, I made second dimesion 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;
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302484%2fcreating-groups-blocks-from-table-based-on-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
In one of the applications I'm working on right now I use something similar to:
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
}
To loop through every value in the grid view and insert into an SQL server. Works pretty quickly for our purposes (~1000 range currently).
The question is not about storing to the database.
– Panagiotis Kanavos
Nov 14 '18 at 14:44
add a comment |
In one of the applications I'm working on right now I use something similar to:
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
}
To loop through every value in the grid view and insert into an SQL server. Works pretty quickly for our purposes (~1000 range currently).
The question is not about storing to the database.
– Panagiotis Kanavos
Nov 14 '18 at 14:44
add a comment |
In one of the applications I'm working on right now I use something similar to:
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
}
To loop through every value in the grid view and insert into an SQL server. Works pretty quickly for our purposes (~1000 range currently).
In one of the applications I'm working on right now I use something similar to:
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
}
To loop through every value in the grid view and insert into an SQL server. Works pretty quickly for our purposes (~1000 range currently).
answered Nov 14 '18 at 14:38
M. GoodmanM. Goodman
767
767
The question is not about storing to the database.
– Panagiotis Kanavos
Nov 14 '18 at 14:44
add a comment |
The question is not about storing to the database.
– Panagiotis Kanavos
Nov 14 '18 at 14:44
The question is not about storing to the database.
– Panagiotis Kanavos
Nov 14 '18 at 14:44
The question is not about storing to the database.
– Panagiotis Kanavos
Nov 14 '18 at 14:44
add a comment |
Based on Export a C# List of Lists to Excel I managed to fast things up by creating generic lists, then pasting it to object lists with two dimensions and then these created lists to excel range. This is way more faster than writing each time to excel cell.
Problem is that Excel does not like List<T>
or either list
. You have to send to excel object[,]
(two dimensional) and since I had just one dimension, I made second dimesion 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;
add a comment |
Based on Export a C# List of Lists to Excel I managed to fast things up by creating generic lists, then pasting it to object lists with two dimensions and then these created lists to excel range. This is way more faster than writing each time to excel cell.
Problem is that Excel does not like List<T>
or either list
. You have to send to excel object[,]
(two dimensional) and since I had just one dimension, I made second dimesion 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;
add a comment |
Based on Export a C# List of Lists to Excel I managed to fast things up by creating generic lists, then pasting it to object lists with two dimensions and then these created lists to excel range. This is way more faster than writing each time to excel cell.
Problem is that Excel does not like List<T>
or either list
. You have to send to excel object[,]
(two dimensional) and since I had just one dimension, I made second dimesion 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;
Based on Export a C# List of Lists to Excel I managed to fast things up by creating generic lists, then pasting it to object lists with two dimensions and then these created lists to excel range. This is way more faster than writing each time to excel cell.
Problem is that Excel does not like List<T>
or either list
. You have to send to excel object[,]
(two dimensional) and since I had just one dimension, I made second dimesion 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;
answered Nov 16 '18 at 11:36
Stana MacalaStana Macala
85
85
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302484%2fcreating-groups-blocks-from-table-based-on-value%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
have you thought about using a
Parallel.For
loop, I did something similar recently and reduced my run-time from 35 min to 5 minutes docs.microsoft.com/en-us/dotnet/standard/parallel-programming/…. it doesn't seem like the order matters so this should work for you– Andrei
Nov 14 '18 at 14:31
How is this related to Microsoft Access?
– Erik A
Nov 14 '18 at 14:42
Excel Interop can only slow things down by at least an order of magnitude, as each call is a cross-process call. If you want to generate an Excel file you don't need a DataGridView either. You can use a library like EPPlus to create real Excel files without using Excel very easily. For example, loading a DataTable into a sheet is as easy as
sheet.Cells.LoadFromDataTable(dataTable);
– Panagiotis Kanavos
Nov 14 '18 at 14:42
@Andrei this won't help at all. You can't access the same Excel instance from multiple clients/threads like this
– Panagiotis Kanavos
Nov 14 '18 at 14:43
Data I load to dgv are either from Access db or SQL Server. Also I have to send it to excel, because of next work afterwards
– Stana Macala
Nov 14 '18 at 14:58