我在内存中有一个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表?
答案 0 :(得分:1)
我认为您需要的是某种内存映射文件实现。
但我相信,.NET 4.0将内置MemoryMappedFile
支持。
您可以使用搜索引擎找到其他.net实现。
答案 1 :(得分:0)
我不确定,但大多数Microsoft自动化对象支持IPersist or IPersistStream,这可能允许您从内存中加载流。
像(伪代码):
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;
将您的字符串填入内存并查看它是否已加载。