Pau*_*sey 1 .net c# excel office-interop reporting-services
假设我有3个字节的数组,每个数组代表一个.xls文件.如何将它们组合成一个包含3张的单个xls文件.SSRS报告非常丰富,包括图表sp oledb不是一个选项.
性能并不重要所以我可以根据需要将它们保存到磁盘,作为最后的手段我甚至可以使用excel宏(如果我知道如何做到这一点).我尝试使用microsodt.office.interop.excel但我只能设法将新工作表添加到文件中,我无法添加现有工作表.
任何帮助,将不胜感激.
在我看来,你只需要一种以编程方式将Byte数组写入WorkBook的方法.
这是一个将byte []作为工作表写入特定WorkBook的方法:
public static void WriteToSheet(string targetBookPath, byte[] fileBytes)
{
try {
object x = Type.Missing;
//Create a temp file to encapsulate Byte array
string tmpPath = IO.Path.ChangeExtension(IO.Path.GetTempFileName(), ".xls");
My.Computer.FileSystem.WriteAllBytes(tmpPath, fileBytes, false);
//Start Excel Application (COM)
Excel.Application xlApp = new Excel.Application();
//Open target book
Excel.Workbook targetBook = xlApp.Workbooks.Open(targetBookPath, x, x, x, x, x, x, x, x, x,
x, x, x, x, x);
//Open temp file with Excel Interop
Excel.Workbook sourceBook = xlApp.Workbooks.Open(tmpPath, x, x, x, x, x, x, x, x, x,
x, x, x, x, x);
//Get a reference to the desired sheet
Excel.Worksheet sourceSheet = (Excel.Worksheet)sourceBook.Worksheets(1);
//Copy the temp sheet into WorkBook specified as "Before" parameter
Excel.Worksheet targetBefore = (Excel.Worksheet)targetBook.Worksheets(1);
try {
sourceSheet.Copy(targetBefore, x);
//Save and Close
sourceBook.Close(false, x, x);
targetBook.Close(true, x, x);
xlApp.Workbooks.Close();
xlApp.Quit();
}
catch (Exception ex) {
Debug.Fail(ex.ToString);
}
finally {
//Release COM objects
// Source
DESTROY(sourceSheet);
DESTROY(sourceBook);
// Target
DESTROY(targetBefore);
DESTROY(targetBook);
// App
DESTROY(xlApp);
}
//Kill the temp file
My.Computer.FileSystem.DeleteFile(tmpPath);
}
catch (Exception ex) {
Debug.Fail(ex.ToString);
}
}
Run Code Online (Sandbox Code Playgroud)
DESTROY方法释放COM内容,这非常重要:
public static void DESTROY(object o)
{
try {
System.Runtime.InteropServices.Marshal.ReleaseComObject(o);
}
catch (Exception ex) {
Debug.Fail(ex.ToString);
ErrorLog.Write(ex.ToString);
}
finally {
o = null;
}
}
Run Code Online (Sandbox Code Playgroud)
如果我理解正确,你需要做的就是:
| 归档时间: |
|
| 查看次数: |
2871 次 |
| 最近记录: |