使用 C# microsoft.office.interop.excel 进行图像处理

问题描述 投票:0回答:0

我是 C# 的新手。我正在做 Excel 自动化。我正在将数据从数据库打印到 excel 并在电子表格中显示此 excel。但是我在数据库中有字节类型的图像数据,打印图像时遇到问题。我通过sql查询访问数据库中的数据。我写的循环只返回字符串。两者都在我的 sql 查询中工作。

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();
                }

            }
c# sql-server excel office-interop office-automation
© www.soinside.com 2019 - 2024. All rights reserved.