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++;
}
}
Related
I am writing a program for connect 4 that works very well.
Only problem is that in visual studio the method
public static void Display(char[,] board)
{
Console.Clear();
for (int i = 1; i < 8; i++)
{
Console.Write(" " + i);
}
Console.WriteLine();
for (int j = 0; j < 15; j++)
{
Console.Write("_");
}
Console.WriteLine();
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 7; j++)
{
Console.Write("|" + board[i, j]);
}
Console.WriteLine('|');
}
for (int j = 0; j < 15; j++)
{
Console.Write("¯");
}
Console.WriteLine();
}
prints the last for loop too low. It is inadequate.
It should be like that (works in repl)
but gets printed like that (in VS)
I tried to use ¯ instead of ‾ but it just printed out question marks ??????????
Why not using the good old Box Drawing characters?
You will have an output like this:
public static void Display(char[,] board)
{
Console.Clear();
Console.Write(" ");
for (int i = 1; i < 8; i++)
{
Console.Write(" " + i + " ");
}
Console.WriteLine();
Console.Write("┌");
for (int j = 0; j < 6; j++)
{
Console.Write("───┬");
}
Console.WriteLine("───┐");
for (int i = 0; i < 6; i++)
{
for (int j = 0; j < 7; j++)
{
Console.Write("│" + " " + board[i, j] + " ");
}
Console.WriteLine("│");
Console.Write(i < 5 ? "├───┼" : "└───┴");
for (int j = 0; j < 5; j++)
{
Console.Write(i < 5 ? "───┼" : "───┴");
}
Console.WriteLine(i < 5 ? "───┤" : "───┘");
}
Console.WriteLine();
}
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 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
I want to find that a given matrix is a sub matrix of the other.
I have tried below piece of code but I am not sure that it would work:-
for (int i = 0; i < a.length - b.length + 1; i++) {
for (int j = 0; j < a[0].length - b[0].length + 1; j++) {
boolean submatrix = true; // at start we assume we have a submatrix
for (int k = 0; k < b.length; ++k) {
for (int l = 0; l < b[0].length; ++l) {
if (a[i + k][j + l] == b[k][l]) {
Console.WriteLine("a[" + (i + k) + "][" + (j + l) + "] = b[" + k + "][" + l + "]");
} else {
submatrix = false; // we found inequality, so it's not a submatrix
}
}
}
if (submatrix) {
Console.WriteLine("Found subatrix at " + i + "," + j + ".");
}
}
}
Please suggest??
Your suggested method is correct, there are only a few syntax and control flow issues which I've fixed.
It is important to point out that this method is only useful for detecting a submatrix of a 2D matrix, not any dimension matrix.
I assumed the datatype is a jagged array of int, though it can easily be changed.
private static bool IsSubMatrix(int[][] a, int[][] b)
{
for (int i = 0; i < a.Length - b.Length + 1; i++)
{
for (int j = 0; j < a[0].Length - b[0].Length + 1; j++)
{
bool found = true;
for (int k = 0; k < b.Length; ++k)
{
for (int l = 0; l < b[0].Length; ++l)
{
if (a[i + k][j + l] != b[k][l])
{
found = false;
break;
}
}
if (!found) break;
}
if (found) return true;
}
}
return false;
}
This is probably also not the fastest implementation.