0

I have a datagridview which I want to export that to excel. It works perfectly but it has a problem in excel. I have some columns which named like 7/8 7/9 .... in sql it shows them like [7/8] [7/9] .... but when I export that to excel those columns names will show with the month name like : 8-jul 9-jul but I don't want them to show like that.

I want to show exactly like columns names in sql.

What should I do?

Any help will be appreciated

1
  • Are you using Microsoft.Office.Interop.Excel? Commented Nov 18, 2012 at 19:18

4 Answers 4

1

If you are using Microsoft.Office.Interop.Excel, you can set numberformat property of the Cell to @ (which is for text) and it will hold the appropriate value, try this:

oSheet.Cells[rowCount, columnCount].numberformat = "@";
oSheet.Cells[rowCount, columnCount] = "7/9";

EDIT According to comment, you should use it like this:

for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) 
{ 
       for (int j = 0; j < dataGridView1.Columns.Count; j++) 
       {
             if(j+1 == 5 && i!=1) //value of the cell where you put values like 7/8 and it's not the column name cell
                   worksheet.Cells[i + 2, j + 1].numberformat = "@";
             worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString(); 
       } 
}

EDIT2 Since it is a column name you can use this:

for (int j = 0; j < dataGridView1.Columns.Count; j++) 
{
    worksheet.Cells[1, j + 1].numberformat = "@"; 
    worksheet.Cells[1, j + 1].dataGridView1.Columns[i].Name
}

for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) 
{ 
       for (int j = 0; j < dataGridView1.Columns.Count; j++) 
       {
             worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString(); 
       } 
}
Sign up to request clarification or add additional context in comments.

7 Comments

Adding ' at the beginning of the string also works (just like in Excel)
@ytoledano Click on the cell that you get that way. You will notice that it's value also has that character at the beginning, so when you copy you will have to be careful ;-)
where? here : for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) { for (int j = 0; j < dataGridView1.Columns.Count; j++) { worksheet.Cells[i + 2, j + 1] = dataGridView1.Rows[i].Cells[j].Value.ToString(); } }`
Autocomplete doesn't show it because it is resolved in runtime, just try it, check the edited answer.
It must work, I tested the code before I have posted. Are you checking the appropriate Excel file?
|
0

it's simple just use a space in [7/8] like [ 7/8]

Comments

0

Use MS ReportViewer to generate excel reports. Or use Aspose library. Or use Devexpress AspxGridView + AspxGridViewExporter

2 Comments

I think I should change something in database. I don't think if it will solve that with MS ReportViewer or the others you mentioned them
How do you export to excel? Can you set cell format with your export tool?
0

Please see this project: https://exportdata.codeplex.com/, it can export large datatable of more than 4,000 rows to excel through datagridview and also allows you to set the excel data property, such as format, style etc.besides. In it, you can set the excel data format to be the one you need.you can give it a try.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.