473,395 Members | 1,616 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Exporting Datagridview to excel using c#.net

hi i'm trying to export datagridview data with image to excel , i'm using the below code, but image cannot be displayed.


Expand|Select|Wrap|Line Numbers
  1. Microsoft.Office.Interop.Excel.Application ExcelApp = new Microsoft.Office.Interop.Excel.Application();
  2.             Microsoft.Office.Interop.Excel._Workbook ExcelBook;
  3.             Microsoft.Office.Interop.Excel._Worksheet ExcelSheet;
  4.             int i = 0;
  5.             int j = 0;
  6.             //create object of excel
  7.             ExcelBook = (Microsoft.Office.Interop.Excel._Workbook)ExcelApp.Workbooks.Add(1);
  8.             ExcelSheet = (Microsoft.Office.Interop.Excel._Worksheet)ExcelBook.ActiveSheet;
  9.             //export header
  10.             for (i = 1; i <= this.dataGridView1.Columns.Count; i++)
  11.             {
  12.                 ExcelSheet.Cells[1, i] = this.dataGridView1.Columns[i - 1].HeaderText;
  13.             }
  14.             //export data
  15.             for (i = 1; i <= this.dataGridView1.RowCount; i++)
  16.             {
  17.                 for (j = 1; j <= dataGridView1.Columns.Count; j++)
  18.                 {
  19.                     ExcelSheet.Cells[i + 1, j] = dataGridView1.Rows[i - 1].Cells[j - 1].Value;
  20.                 }
  21.             }
  22.             ExcelApp.Visible = true;
  23.             //set font Khmer OS System to data range
  24.             Microsoft.Office.Interop.Excel.Range myRange = ExcelSheet.get_Range(ExcelSheet.Cells[1, 1], ExcelSheet.Cells[this.dataGridView1.RowCount + 1, this.dataGridView1.Columns.Count]);
  25.             Microsoft.Office.Interop.Excel.Font x = myRange.Font;
  26.             x.Name = "Arial";
  27.             x.Size = 10;
  28.             //set bold font to column header
  29.             myRange = ExcelSheet.get_Range(ExcelSheet.Cells[1, 1], ExcelSheet.Cells[1, this.dataGridView1.Columns.Count]);
  30.             x = myRange.Font;
  31.             x.Bold = true;
  32.             //autofit all columns
  33.  
  34.             myRange.EntireColumn.AutoFit();
  35.             //
  36.             ExcelSheet = null;
  37.             ExcelBook = null;
  38.             ExcelApp = null;
pls help me .............
Dec 3 '12 #1
5 5499
Rabbit
12,516 Expert Mod 8TB
I don't see where in your code you try to insert your image.
Dec 3 '12 #2
i have 5 columns in datagridview and 5th column is imagecolumn. already i have inserted image to datagridview. when i export datagridview to excel, data's are printing but image not displaying. this code is for export button_click..
Dec 3 '12 #3
Rabbit
12,516 Expert Mod 8TB
I still don't see where you account for image data anywhere in your code. Image objects are different from text data, you have to treat it differently.
Dec 3 '12 #4
Expand|Select|Wrap|Line Numbers
  1. private void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
  2. {
  3.  double shapes1== Convert.ToDouble(dataGridView1.CurrentRow.Cells[1].Value);
  4.  
  5. if(shapes1==030)
  6. {
  7.   Bitmap img2 = Properties.Resources.IT_030;
  8.  dataGridView1.CurrentRow.Cells[5].Value = img2;
  9. }
  10. }
i have combobox in column1 if combobox text is ie.,(030) respective image will display in imagecolumn from project resources. this is how i'm inserting imagecolumn value of datagridview.
Dec 4 '12 #5
Rabbit
12,516 Expert Mod 8TB
That's half the puzzle. You have to treat images differently to get images to show up in the datagrid. You also have to treat images differently to get them to show up in excel.
Dec 4 '12 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Ali Eghtebas | last post by:
Hi, I tried this demo application in here (watch for any line breaks in the URL): (http://support.crystaldecisions.com/communityCS/FilesAndUpdates/vbnet_win_a dodotnet.exe.asp When exporting...
2
by: deko | last post by:
I use a complied query to export to Excel like this: SELECT * INTO . FROM tblExcelData; But I have a situation where I need to export several tables into the same worksheet. The idea is to...
1
by: Matt | last post by:
I have code that generates a dataset and sends the results to the user and automatically opens Excel when the link is clicked. It works fine on my own machine and one server but I get the following...
1
by: ad | last post by:
I use the code below to export the content of a data set to Excel, the code come form http://www.dotnetjohn.com/articles.aspx?articleid=36 But it always use the web form's name as the default...
2
by: Rez | last post by:
Hi Guys, I have a problem with exporting to excel from a modal dialog box. Basically, i have an ASPX page that gathers user criteria, and passes it to a dialogbox (showModalDialog), There is a...
1
by: Mustufa Baig | last post by:
I have an ASP.NET website where I am showing off crystal reports to users by exporting them to pdf format. Following is the code: ---------------- 1 Private Sub ExportReport() 2 Dim oStream...
0
by: Microsoft NewsGroups | last post by:
I am creating a TAB delimited file with .XLS extention using the Response.ContentType = "application/ms-excel" method. Normally I would place this on it's own form and delete all the HTML in the...
7
by: Gal Zilberman | last post by:
Hi I've found a code which should work, but it doesn't. Please Help Imports Microsoft.Office.Interop ' Refence in the Class Dim oExcel As New Object Dim oBook As Object Dim oSheet As Object...
4
by: Tom | last post by:
I have a gridview on all of my web pages in my web app and they all export to excel. I have one page where the gridview is binding to a datatable that i created and only the first column is...
1
by: Doogie | last post by:
Can anoyne tell me why this VBScript will create the file to Excel just fine, but the Excel file will not open up? I am saving it as a xlsx file instead of an xls one and I have the new version of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.