在内存中将 XML 字符串作为 Excel 工作簿打开,而不使用 Windows Forms C# 进行保存

发布于 2024-08-18 22:27:27 字数 3053 浏览 7 评论 0原文

我的内存中有一个 excel 字符串(我构建的);代码看起来像这样:

public static void exportToExcel()
        {
            const string startExcelXML = "<xml version>\r\n<Workbook " +
                  "xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\r\n"
+
                  " xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\n " +
                  "xmlns:x=\"urn:schemas-    microsoft-com:office:" +
                  "excel\"\r\n xmlns:ss=\"urn:schemas-microsoft-com:"
+
                  "office:spreadsheet\">\r\n <Styles>\r\n " +
                  "<Style ss:ID=\"Default\" ss:Name=\"Normal\">\r\n " +
                  "<Alignment ss:Vertical=\"Bottom\"/>\r\n <Borders/>" +
                  "\r\n <Font/>\r\n <Interior/>\r\n <NumberFormat/>" +
                  "\r\n <Protection/>\r\n </Style>\r\n " +
                  "<Style ss:ID=\"BoldColumn\">\r\n <Font " +
                  "x:Family=\"Swiss\" ss:Bold=\"1\"/>\r\n </Style>\r\n " +
                  "<Style     ss:ID=\"StringLiteral\">\r\n <NumberFormat" +
                  " ss:Format=\"@\"/>\r\n </Style>\r\n <Style " +
                  "ss:ID=\"Decimal\">\r\n <NumberFormat/>\r\n </Style>\r\n " +
                  "<Style ss:ID=\"Integer\">\r\n <NumberFormat "
+
                  "ss:Format=\"0\"/>\r\n </Style>\r\n <Style " +
                  "ss:ID=\"DateLiteral\">\r\n <NumberFormat " +
                  "ss:Format=\"dd/mm/yyyy;@\"/>\r\n </Style>\r\n " +
                  "</Styles>\r\n ";
            const string endExcelXML = "</Workbook>";

            int sheetCount = 1;
            StringBuilder sb = new StringBuilder();

            sb.Append(startExcelXML);
            sb.Append("<Worksheet ss:Name=\"Sheet" + sheetCount + "\">");
            sb.Append("<Table>");
            sb.Append("<Row>");
            sb.Append("<Cell ss:StyleID=\"BoldColumn\"><Data ss:Type=\"String\">");
            sb.Append("Home country");
            sb.Append("</Data></Cell>");
            sb.Append("<Cell ss:StyleID=\"BoldColumn\"><Data ss:Type=\"String\">");
            sb.Append("Expatriation Type");
            sb.Append("</Data></Cell>");
            sb.Append("</Row>");
            sb.Append("<Row>");
     sb.Append("<Cell ss:StyleID=\"StringLiteral\">" +
      "<Data ss:Type=\"String\">");
     sb.Append("Singapore");
     sb.Append("</Data></Cell>");
     sb.Append("<Cell ss:StyleID=\"StringLiteral\">" +
      "<Data ss:Type=\"String\">");
     sb.Append("Benchmark");
     sb.Append("</Data></Cell>");
            sb.Append("</Row>");
            sb.Append("</Table>");
            sb.Append(" </Worksheet>");
            sb.Append(endExcelXML);
        }

仅当我物理保存文件时,我才能将文件作为 Excel 工作表打开;但有没有其他方法可以将内存中的 xml 字符串作为 Excel 工作表打开?

I have an excel string (which I built) in memory; the code looks something like this:

public static void exportToExcel()
        {
            const string startExcelXML = "<xml version>\r\n<Workbook " +
                  "xmlns=\"urn:schemas-microsoft-com:office:spreadsheet\"\r\n"
+
                  " xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\n " +
                  "xmlns:x=\"urn:schemas-    microsoft-com:office:" +
                  "excel\"\r\n xmlns:ss=\"urn:schemas-microsoft-com:"
+
                  "office:spreadsheet\">\r\n <Styles>\r\n " +
                  "<Style ss:ID=\"Default\" ss:Name=\"Normal\">\r\n " +
                  "<Alignment ss:Vertical=\"Bottom\"/>\r\n <Borders/>" +
                  "\r\n <Font/>\r\n <Interior/>\r\n <NumberFormat/>" +
                  "\r\n <Protection/>\r\n </Style>\r\n " +
                  "<Style ss:ID=\"BoldColumn\">\r\n <Font " +
                  "x:Family=\"Swiss\" ss:Bold=\"1\"/>\r\n </Style>\r\n " +
                  "<Style     ss:ID=\"StringLiteral\">\r\n <NumberFormat" +
                  " ss:Format=\"@\"/>\r\n </Style>\r\n <Style " +
                  "ss:ID=\"Decimal\">\r\n <NumberFormat/>\r\n </Style>\r\n " +
                  "<Style ss:ID=\"Integer\">\r\n <NumberFormat "
+
                  "ss:Format=\"0\"/>\r\n </Style>\r\n <Style " +
                  "ss:ID=\"DateLiteral\">\r\n <NumberFormat " +
                  "ss:Format=\"dd/mm/yyyy;@\"/>\r\n </Style>\r\n " +
                  "</Styles>\r\n ";
            const string endExcelXML = "</Workbook>";

            int sheetCount = 1;
            StringBuilder sb = new StringBuilder();

            sb.Append(startExcelXML);
            sb.Append("<Worksheet ss:Name=\"Sheet" + sheetCount + "\">");
            sb.Append("<Table>");
            sb.Append("<Row>");
            sb.Append("<Cell ss:StyleID=\"BoldColumn\"><Data ss:Type=\"String\">");
            sb.Append("Home country");
            sb.Append("</Data></Cell>");
            sb.Append("<Cell ss:StyleID=\"BoldColumn\"><Data ss:Type=\"String\">");
            sb.Append("Expatriation Type");
            sb.Append("</Data></Cell>");
            sb.Append("</Row>");
            sb.Append("<Row>");
     sb.Append("<Cell ss:StyleID=\"StringLiteral\">" +
      "<Data ss:Type=\"String\">");
     sb.Append("Singapore");
     sb.Append("</Data></Cell>");
     sb.Append("<Cell ss:StyleID=\"StringLiteral\">" +
      "<Data ss:Type=\"String\">");
     sb.Append("Benchmark");
     sb.Append("</Data></Cell>");
            sb.Append("</Row>");
            sb.Append("</Table>");
            sb.Append(" </Worksheet>");
            sb.Append(endExcelXML);
        }

I able to open the file as excel sheet only if I save the file physically; but is there any other way to just open the xml string from the memory as excel sheet?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

诗酒趁年少 2024-08-25 22:27:27

我认为您需要的是某种内存映射文件实现。

不过,我相信 .NET 4.0 将具有内置的 MemoryMappedFile 支持。

您也许可以使用搜索引擎找到其他 .net 实现。

I think what you'll need is a memory mapped file implementation of some kind.

I believe, though, that .NET 4.0 will have built-in MemoryMappedFile support.

You may be able to find other .net implementations using a search engine.

天邊彩虹 2024-08-25 22:27:27

我不确定,但大多数 Microsoft 自动化对象支持 IPersist 或 IPersistStream< /a>,这可以让您从内存中的流加载。

类似于(伪代码):

xl = new Office.ExcelApplication;
(xl as IPersistStream).Load(myMemoryStream);

您可以使用 shell 帮助程序函数 CreateStreamOnHGlobal 创建 IStream 一些内存的包装器:

functionCreateStreamOnMemory(pData: Pointer; nCount: DWORD): IStream;
var
    hMem: HGLOBAL;
    dwError: DWORD;
    p: Pointer;
begin
    hMem := GlobalAlloc(GMEM_MOVEABLE or GMEM_NODISCARD, nCount); 
       //CreateStreamOnHGlobal says "The handle must be 
       //allocated as movable and nondiscardable."
    if hMem = 0 then
        RaiseLastWin32Error;

    p := GlobalLock(hMem);
    if p = nil then
    begin
        dwError := GetLastError;
        GlobalFree(hMem);
        raise EWin32Error.Create('Could not lock global memory object: '+SysErrorMessage(dwError));
    end;
    try
        CopyMemory(p, pData, nCount);
    finally
        GlobalUnlock(hMem);
    end;

    OleCheck(CreateStreamOnHGlobal(hMem, True, Result)); //Because we pass True, the stream will take care of freeing the HGLOBAL when the stream is released
end;

将字符串填充到内存中并查看它是否加载。

i don't know for sure, but most Microsoft automation objects support IPersist or IPersistStream, which may let you load from a stream in memory.

Something like (pseudo-code):

xl = new Office.ExcelApplication;
(xl as IPersistStream).Load(myMemoryStream);

You can use the shell helper function CreateStreamOnHGlobal to create an IStream wrapper around some memory:

functionCreateStreamOnMemory(pData: Pointer; nCount: DWORD): IStream;
var
    hMem: HGLOBAL;
    dwError: DWORD;
    p: Pointer;
begin
    hMem := GlobalAlloc(GMEM_MOVEABLE or GMEM_NODISCARD, nCount); 
       //CreateStreamOnHGlobal says "The handle must be 
       //allocated as movable and nondiscardable."
    if hMem = 0 then
        RaiseLastWin32Error;

    p := GlobalLock(hMem);
    if p = nil then
    begin
        dwError := GetLastError;
        GlobalFree(hMem);
        raise EWin32Error.Create('Could not lock global memory object: '+SysErrorMessage(dwError));
    end;
    try
        CopyMemory(p, pData, nCount);
    finally
        GlobalUnlock(hMem);
    end;

    OleCheck(CreateStreamOnHGlobal(hMem, True, Result)); //Because we pass True, the stream will take care of freeing the HGLOBAL when the stream is released
end;

Stuff your string in the memory and see if it loads.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文