I need three columns from the datagridview to export to columns a, c, and i in excel. All the data in the dataGridView needs to be visible there, but only the first three rows need to be exported.
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
objexcelapp.Cells[1, i] = dataGridView1.Columns.Count - 1; i++ ;
}
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
if (dataGridView1.Rows[i].Cells[j].Value != null)
{
objexcelapp.Cells[i + 11, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
}
If you only need the first 3 rows, then count to 3 instead of all rows. I don't understand how you ended up with all rows
for (int i = 1; i < dataGridView1.Columns.Count + 1; i++)
{
objexcelapp.Cells[1, i] = dataGridView1.Columns.Count - 1; i++;
}
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
if (dataGridView1.Rows[i].Cells[j].Value != null)
{
objexcelapp.Cells[i + 11, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
}
}
}
Update
To write to excel check this post
Related
Can anyone help me I want my C# code to start loop and print at row number 5 in my Excel file.
Here is my current code:
DataColumnCollection dataColumnCollection = dataTable.Columns;
for (int i = 1; i <= dataTable.Rows.Count + 1; i++)
{
for (int j = 1; j <= dataTable.Columns.Count; j++)
{
if (i == 1)
excelApplication.Cells[i, j] = dataColumnCollection[j - 1].ToString();
else
excelApplication.Cells[i, j] = dataTable.Rows[i - 2][j - 1].ToString();
}
Here is my sample data:
sample data
And here is what I want the file to look like:
sample result
first of all arrays and lists the like start at 0 in most cases.
In your case if you want to start at row 5, simply increment the start with 5.
const int start = 5;
int end = datadataTable.Rows.Count;
for (int i = 0 + start; i < end; i++) // loop from 5..end
{
for (int j = 1; j <= dataTable.Columns.Count; j++)
{
if (i == 1)
excelApplication.Cells[i, j] = dataColumnCollection[j - 1].ToString();
else
excelApplication.Cells[i, j] = dataTable.Rows[i - 2][j - 1].ToString();
}
how can we change the DataType = CellValues.String, of each excel column from c#.
for (int index = 0; index < NoOfRecords; index++)
{
for (int j = 0; j < colCount; j++)
{
mWSheet1.Cells[(rowCount) + index, j + 1] =Convert.ToString(ResultsData.Rows[index][j].ToString());
}
}
put a string format("'"+) in frond of each collection
for (int index = 0; index < NoOfRecords; index++)
{
for (int j = 0; j < colCount; j++)
{
mWSheet1.Cells[(rowCount) + index, j + 1] ="'"+Convert.ToString(ResultsData.Rows[index][j].ToString());
}
}
I have been trying to separate two numbers using "." but in Excel it sets ",". How can I fix it?
int col= 2;
for (int i = 0; i < proc; i++)
{
for (int j = 0; j < mac; j++)
{
wse.Cells[1, col] = (i + 1).ToString()+"." + (j + 1).ToString();
col++;
}
}
I have a function to calculate the costMatrix like this code below.
public double[,] makeCostMatrixClassic(List<PointD> firstSeq, List<PointD> secondSeq)
{
double[,] costMatrix = new double[firstSeq.Count, secondSeq.Count];
costMatrix[0, 0] = Math.Round(this.getEuclideanDistance(firstSeq[0], secondSeq[0]), 3);
// For D(n,1)
for (int i = 0; i < firstSeq.Count; i++)
{
for (int j = 0; j <= i; j++)
{
costMatrix[i, 0] += Math.Round(this.getEuclideanDistance(firstSeq[j], secondSeq[0]), 3);
}
}
// For D(1,m)
for (int i = 0; i < secondSeq.Count; i++)
{
for (int j = 0; j <= i; j++)
{
costMatrix[0, i] += Math.Round(this.getEuclideanDistance(firstSeq[0], secondSeq[j]), 3);
}
}
// For D(n,m)
for (int i = 1; i < firstSeq.Count; i++)
{
for (int j = 1; j < secondSeq.Count; j++)
{
double min = this.findMin(costMatrix[i - 1, j - 1], costMatrix[i - 1, j], costMatrix[i, j - 1]);
costMatrix[i, j] = min + Math.Round(this.getEuclideanDistance(firstSeq[i], secondSeq[j]), 3);
}
}
return costMatrix;
}
For the 3rd loop (n,m), how could i improve its performance if the "count" of each Sequence is 1 million points.
I am trying to export my datagridview contents to a excel file which exist in application.startuppath. The problem I am facing is that whenever I export my grid content to excel, it overwrites the previous content. What I would like to do is append to my excel file and not overwrite my existing content.
Here is my code:
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Open(Application.StartupPath+"excelfile.xls");
Microsoft.Office.Interop.Excel._Worksheet worksheet = null;
app.Visible = false;
worksheet = workbook.Sheets["Sheet1"];
worksheet = workbook.ActiveSheet;
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
if (dataGridView1.Rows[i].Cells[j].Value != null)
worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
else
worksheet.Cells[i + 2, j + 1] = "";
}
}
workbook.Close(SaveChanges: true);
app.Quit();
EDIT:
Updated code : thanks to #Alex Bell
int _offset = worksheet.UsedRange.Rows.Count;
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 1; j < dataGridView1.Columns.Count; j++)
{
if (dataGridView1.Rows[i].Cells[j].Value != null)
worksheet.Cells[i + 2 + _offset, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
else
worksheet.Cells[i + 2 + _offset, j + 1] = "";
}
}
Resolved my problem of appending data but another small problem is every time I update it leaves a blank row between old rows and new rows.
Pertinent to your case, you may use int _offset = worksheet.UsedRange.Rows.Count as row offset in your loop:
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
for (int j = 0; j < dataGridView1.Columns.Count; j++)
{
if (dataGridView1.Rows[i].Cells[j].Value != null)
worksheet.Cells[i + 2 + _offset, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString();
else
worksheet.Cells[i + 2 + _offset, j + 1] = "";
}
}
Hope this will help. Regards,