1

I'm new to C#. I'm doing Excel automation. I am printing data from database to excel and showing this excel in spreadsheet. But I have image data of type byte in database and I am having trouble printing the image. I access the data in the database through sql queries. The loop I wrote only returns string. Both work in my sql query.

string currentdatetime = DateTime.Now.ToString("yyyyMMddHHmmss");
            string connectionString = Settings.Default["ConnectionString"].ToString();
            string queryString = Settings.Default["Query"].ToString();
            SaveFileDialog save = new SaveFileDialog();
            save.OverwritePrompt = true;
            save.Filter = "Excel Dosyası |.xlsx| Excel Dosyası |.xls";
            if (Settings.Default.Query != string.Empty || Settings.Default.Query != null)
            {

                if (save.ShowDialog() == DialogResult.OK)
                {
                    try
                    {



                        // Connect to the SQL Server database and retrieve the data you want to export
                        using (SqlConnection connection = new SqlConnection(connectionString))
                        {
                            connection.Open();
                            using (SqlCommand command = new SqlCommand(queryString, connection))
                            {
                                using (SqlDataReader reader = command.ExecuteReader())
                                {
                                    // Create a new Excel application and workbook
                                    Application excelApp = new Application();
                                    Workbook excelWorkbook = excelApp.Workbooks.Add();
                                    Worksheet excelWorksheet = excelWorkbook.Worksheets[1];

                                    // Add the headers to the first row
                                    int col = 1;
                                    for (int i = 0; i < reader.FieldCount; i++)
                                    {
                                        excelWorksheet.Cells[1, col].Value2 = reader.GetName(i);
                                        col++;
                                    }

                                    //Iterate through the rows of data and insert them into the worksheet, starting from the second row
                                    int row = 2;
                                    while (reader.Read())
                                    {
                                        col = 1;
                                        for (int i = 0; i < reader.FieldCount; i++)
                                        {
                                            //excelWorksheet.Cells[row, col].Value2 = reader[i];
                                            excelWorksheet = excelWorkbook.Sheets[i] as Worksheet;
                                            col++;
                                        }
                                        row++;
                                    }

                                    // Save the workbook and close the Excel application
                                    excelWorkbook.SaveAs(save.FileName);
                                    excelWorkbook.Close();
                                    excelApp.Quit();
                                }
                            }
                        }
                        using (FileStream stream = new FileStream(save.FileName, FileMode.Open))
                        {
                            ExcelSpread.LoadDocument(stream);
                        }
                        Cursor.Current = Cursors.Default;
                        //var spr = ExcelSpread.Document;
                        //spr.LoadDocument(save.FileName);
                    }

                    catch (Exception exception)
                    {
                        MessageBox.Show(exception.Message);

                    }
                }
            }
            else
            {
                //XtraMessageBox.Show("Lütfen önce Veritaban Ayarları Sekmesinden Sorgu Yaz Bölümünden Kullanacak Olduğunuz Sorguyu Yazınız.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                if (XtraMessageBox.Show("Lütfen önce Veritaban Ayarları Sekmesinden Sorgu Yaz Bölümünden Kullanacak Olduğunuz Sorguyu Yazınız.", "Hata", MessageBoxButtons.OK, MessageBoxIcon.Warning) == DialogResult.OK)
                {
                    Forms.WriteQuery wq = new Forms.WriteQuery();
                    wq.Show();
                }

            }
Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45

0 Answers0