C#C
C#3y ago
yumo

❔ Missing Reference

This is my code:
private void button1_Click(object sender, EventArgs e)
        {
            _Application excelApp;
            _Workbook excelWorkbook;
            _Worksheet excelWorksheet;

            try
            {
                excelApp = new Microsoft.Office.Interop.Excel.Application();
                excelWorkbook = excelApp.Workbooks.Add(Type.Missing);
                excelWorksheet = null;

                excelWorksheet = excelWorkbook.Sheets["Folha1"];
                excelWorksheet = excelWorkbook.ActiveSheet;
                excelWorksheet.Name = "Dados exportados";

                for (int j = 0; j < dataGridViewAcoes.Rows.Count; j++)
                {
                    for (int k = 0; k < dataGridViewAcoes.Columns.Count; k++)
                    {
                        if (dataGridViewAcoes.Columns[k] is DataGridViewImageColumn)
                        {
                            // Column contains an image
                            byte[] imageData = (byte[])dataGridViewAcoes.Rows[j].Cells[k].Value;
                            if (imageData != null)
                            {
                                // Convert byte array to image
                                MemoryStream ms = new MemoryStream(imageData);
                                Image image = Image.FromStream(ms);

                                // Insert image into cell
                                Excel.Range range = (Excel.Range)excelWorksheet.Cells[j + 2, k + 1];
                                range.RowHeight = image.Height;
                                if (range.RowHeight < 0 || range.RowHeight > 409)
                                {
                                    // If row height is outside acceptable range, set to default value
                                    range.RowHeight = excelWorksheet.DefaultRowHeight;
                                }
                                range.ColumnWidth = 15;
                                Clipboard.SetImage(image);
                                excelWorksheet.Paste(range, false);
                            }
                        }
                        else
                        {
                            // Column does not contain an image
                            excelWorksheet.Cells[j + 2, k + 1] = dataGridViewAcoes.Rows[j].Cells[k].Value.ToString();
                        }
                    }
                }


                SaveFileDialog exportExcelFile = new SaveFileDialog();
                exportExcelFile.FileName = "Ações";
                exportExcelFile.DefaultExt = "xlsx";

                if (exportExcelFile.ShowDialog() == DialogResult.OK)
                {
                    excelWorkbook.SaveAs(exportExcelFile.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
                }
                excelWorksheet.Columns.AutoFit();
            }
            catch (Exception)
            {
                throw;
            }
        }

And this is the error that's giving:

Severity Code Description Project Project Classification File Line Suppression Status
Error CS1061 '_Worksheet' does not contain a definition for "DefaultRowHeight" and no extension method "DefaultRowHeight" could be found that accepts a first argument of type '_Worksheet' (are you forgetting to use a directive or an assembly reference? ) PontosAbertos 1 C:\Users\infprog\Desktop\PA\PontosAbertos\Acoes.cs 299 Active

But i am using: using Excel = Microsoft.Office.Interop.Excel;
Was this page helpful?