TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Shalini rajendran
NA
21
10.7k
Exporting Datagridview data with Images to Excel...
Apr 22 2015 5:16 AM
Hi Everyone,
I have datagridview in that 17 columns are text columns and 5 columns are Image columns,when I'm trying to export whole gridview data's along with images to EXCEL,getting
"HRESULT: 0x800A03EC"
Error..While debugging the code I'm getting the error on image column not able to export..
Can anybody help us to resolve this issue..
Here is my code which i used:
private void btnexportexcel_Click(object sender, EventArgs e)
{
try
{
SaveFileDialog savefile = new SaveFileDialog();
savefile.Filter = "Excel (*.xls)|*.xls";
if (savefile.ShowDialog() == DialogResult.OK)
{
if (!savefile.FileName.Equals(string.Empty))
{
FileInfo finfo = new FileInfo(savefile.FileName);
if (finfo.Extension.Equals(".xls"))
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(2) ;
int i = 0;
int j = 0;
for (i = 0; i <= dataGridView1.RowCount - 1; i++)
{
for (j = 0; j <= dataGridView1.ColumnCount - 1; j++)
{
DataGridViewCell cell = dataGridView1[j, i];
xlWorkSheet.Cells.Borders.LineStyle = Excel.XlLineStyle.xlContinuous;
xlWorkSheet.Columns.AutoFit();
if (cell.Value.GetType() == typeof(Bitmap))
{
string image =Convert.ToString(dataGridView1.CurrentRow.Cells[i].Value);
Excel.Range oRange = (Excel.Range)xlWorkSheet.Cells[i + 1, j + 1];
float Left = (float)((double)oRange.Left);
float Top = (float)((double)oRange.Top);
const float ImageSize = 32;
xlWorkSheet.Shapes.AddPicture(image, Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, Left, Top, ImageSize, ImageSize);
oRange.RowHeight = ImageSize + 2;
}
else
{
xlWorkSheet.Cells[i + 1, j + 1] = cell.Value;
}
}
}
xlWorkBook.SaveAs(savefile.FileName, Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue,
Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
MessageBox.Show("Excel file created , you can find the file " + savefile.FileName);
}
else
{
MessageBox.Show("Invalid file type");
}
}
else
{
MessageBox.Show("You did pick a location " + "to save file to");
}
}
}
catch (Exception ex)
{
MessageBox.Show("Exception Occured", ex.Message);
}
}
Reply
Answers (
4
)
html editor
[C#] DateTimePicker: MinDate e Range Hour