K
Kanade Sevilla
Guest
I want to export a datagridview to excel, everything is fine, until the moment I get to the image.
I can't show the image in excel.
My images are in the datagridview and I have them in a MYSQL table with longblob extension.
**What am I falling? or Is there any other way to achieve what I want?
This is my code:
private void pbExcel_Click(object sender, EventArgs e)
{
exportexcel(dgwObservaciones);
}
void exportexcel(DataGridView dgwObservaciones)
{
Excel.Application oXL; //Se va usar Excel e interactuar con la aplicación
Excel._Workbook oWB; //Crea el libro
Excel._Worksheet oSheet;
try
{
//Inicia Excel
oXL = new Excel.Application();
oXL.Visible = true;
//Obtiene el libro y la hoja con la que se va a trabajar
oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
//Ancho de columnas
oSheet.get_Range("A1").ColumnWidth = 30;
oSheet.get_Range("B1", "D1").ColumnWidth = 50;
//Empieza llenado
oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Worksheets.get_Item(1);
//Recorremos el DataGridView rellenando la hoja de trabajo
for (int i = 0; i < dgwObservaciones.Rows.Count; i++)
{
for (int j = 0; j < dgwObservaciones.Columns.Count; j++)
{
DataGridViewCell cell = dgwObservaciones[j, i];
if (cell.Value.GetType() == typeof(byte[]))
{
string image = Convert.ToString(dgwObservaciones.CurrentRow.Cells.Value);
Excel.Range oRange = (Excel.Range)oSheet.Cells[i + 1, j + 1];
float Left = (float)((double)oRange.Left);
float Top = (float)((double)oRange.Top);
const float ImageSize = 32;
oSheet.Shapes.AddPicture(oSheet.Cells[i + 1, j + 1], Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, Left, Top, ImageSize, ImageSize);
oRange.RowHeight = ImageSize + 2;
}
else
{
oSheet.Cells[i + 1, j + 1] = cell.Value;
}
}
}
}
catch (Exception theException)
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat(errorMessage, theException.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, theException.Source);
MessageBox.Show(errorMessage, "Error");
}
}
Continue reading...
I can't show the image in excel.
My images are in the datagridview and I have them in a MYSQL table with longblob extension.
**What am I falling? or Is there any other way to achieve what I want?
This is my code:
private void pbExcel_Click(object sender, EventArgs e)
{
exportexcel(dgwObservaciones);
}
void exportexcel(DataGridView dgwObservaciones)
{
Excel.Application oXL; //Se va usar Excel e interactuar con la aplicación
Excel._Workbook oWB; //Crea el libro
Excel._Worksheet oSheet;
try
{
//Inicia Excel
oXL = new Excel.Application();
oXL.Visible = true;
//Obtiene el libro y la hoja con la que se va a trabajar
oWB = (Excel._Workbook)(oXL.Workbooks.Add(Missing.Value));
oSheet = (Excel._Worksheet)oWB.ActiveSheet;
//Ancho de columnas
oSheet.get_Range("A1").ColumnWidth = 30;
oSheet.get_Range("B1", "D1").ColumnWidth = 50;
//Empieza llenado
oSheet = (Microsoft.Office.Interop.Excel.Worksheet)oWB.Worksheets.get_Item(1);
//Recorremos el DataGridView rellenando la hoja de trabajo
for (int i = 0; i < dgwObservaciones.Rows.Count; i++)
{
for (int j = 0; j < dgwObservaciones.Columns.Count; j++)
{
DataGridViewCell cell = dgwObservaciones[j, i];
if (cell.Value.GetType() == typeof(byte[]))
{
string image = Convert.ToString(dgwObservaciones.CurrentRow.Cells.Value);
Excel.Range oRange = (Excel.Range)oSheet.Cells[i + 1, j + 1];
float Left = (float)((double)oRange.Left);
float Top = (float)((double)oRange.Top);
const float ImageSize = 32;
oSheet.Shapes.AddPicture(oSheet.Cells[i + 1, j + 1], Microsoft.Office.Core.MsoTriState.msoFalse, Microsoft.Office.Core.MsoTriState.msoCTrue, Left, Top, ImageSize, ImageSize);
oRange.RowHeight = ImageSize + 2;
}
else
{
oSheet.Cells[i + 1, j + 1] = cell.Value;
}
}
}
}
catch (Exception theException)
{
String errorMessage;
errorMessage = "Error: ";
errorMessage = String.Concat(errorMessage, theException.Message);
errorMessage = String.Concat(errorMessage, " Line: ");
errorMessage = String.Concat(errorMessage, theException.Source);
MessageBox.Show(errorMessage, "Error");
}
}
Continue reading...