将Yogesh.ExcelXmlWorkBook转换为字节数组?

huangapple go评论80阅读模式
英文:

How to Convert Yogesh.ExcelXmlWorkBook To byte Array?

问题

我有一个将 SQL 数据读取器转换为 Yogesh.ExcelXmlWorkBook 并将其保存到指定文件夹的方法,但我想直接将其存储到具有二进制格式的数据库中。请问如何将它转换为字节数组并直接保存到数据库?非常感谢。

英文:

Hello I Have A Method That Converts Sql Data Reader To Yogesh.ExcelXmlWorkBook And Save It Into An Specified Folder,But I Want To Store It Directly To Database With Binary Format,Could You Please Help Me How Can I Convert It to Byte Array And Save It Directly Into Database? Thanks A Lot.

public static string ExportToXML(int reportCode, SqlDataReader sqlReader, bool hasHeader)
        {
            string randomName = "";
            try
            {
                if (reportDefaultPath == "")
                {
                   // reportDefaultPath = HttpContext.Current.Server.MapPath("~/Files/ReportResult/{0}");
                    reportDefaultPath = System.Configuration.ConfigurationSettings.AppSettings["PicPath"].ToString() + "\\ReportResult\\{0}";   
                }

                ExcelXmlWorkbook book = new ExcelXmlWorkbook();
                book.Properties.Author = "Raahabr";
                Yogesh.ExcelXml.Worksheet sheet = book[0];
                sheet.Name = "";
                sheet.FreezeTopRows = 4;
              //  sheet.Border.LineStyle = Yogesh.ExcelXml.Borderline.Continuous;
              //  sheet.Border.Sides = BorderSides.Bottom;

                int startCol = 3;
                int currentRow = 3;

                int colCount = sqlReader.FieldCount;
                if (hasHeader)
                {
                    ReportBOL rbol = new ReportBOL();
                    Dictionary<string, string> cNames = rbol.getColumnNames(reportCode);

                    for (int i = 0; i < colCount; i++)
                    {
                        string head = sqlReader.GetName(i);
                        string value = (cNames.ContainsKey(head)) ? cNames[head] : head;
                        if (value.ToString() == "")
                            value = head;
                        sheet[currentRow + i, currentRow].Value = value;
                        sheet[currentRow + i, currentRow].Border.LineStyle = Borderline.Continuous;
                        sheet[currentRow + i, currentRow].Border.Sides = BorderSides.All;
                    }
                }
                currentRow++;

                if (sqlReader.HasRows)
                {
                    while (sqlReader.Read())
                    {
                        for (int i = 0; i < colCount; i++)
                        {
                            sheet[startCol + i, currentRow].Value = sqlReader[i].ToString();
                            sheet[startCol + i, currentRow].Border.LineStyle = Borderline.Continuous;
                            sheet[startCol + i, currentRow].Border.Sides = BorderSides.All;
                        }
                        currentRow++;
                    }
                }
                randomName = getRandomName();
                book.Export(string.Format(reportDefaultPath, randomName));
                var tools = new Tools();
                book.DeleteSheet(0);
            }
            catch (Exception ex)
            {
               // System.IO.File.WriteAllText(HttpContext.Current.Server.MapPath("~/err.txt"), randomName + ";" + reportDefaultPath + "\r\n" + ex.Message + "\r" + ex.StackTrace);
            }
            finally
            {
                sqlReader.Close();
                sqlReader.Dispose();
            }
            return randomName;
        }```

</details>


# 答案1
**得分**: 0

将您的输出发送到一个流:

https://documentation.help/Yogesh.ExcelXml/6cfd5317-629d-b05e-cffe-6d4cadbb191b.htm(搜索“导出”)

https://documentation.help/Yogesh.ExcelXml/6f5d5ce5-edfb-9517-1866-6066cad5d8b9.htm(流导出文档)

将流转换为字节数组:

https://stackoverflow.com/questions/221925/creating-a-byte-array-from-a-stream

<details>
<summary>英文:</summary>

Send your output to a stream:

https://documentation.help/Yogesh.ExcelXml/6cfd5317-629d-b05e-cffe-6d4cadbb191b.htm (search &quot;export&quot;)

https://documentation.help/Yogesh.ExcelXml/6f5d5ce5-edfb-9517-1866-6066cad5d8b9.htm (export to stream documentation)

Convert the stream to a byte array:

https://stackoverflow.com/questions/221925/creating-a-byte-array-from-a-stream

</details>



huangapple
  • 本文由 发表于 2023年5月22日 15:08:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76303738.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定