2012-11-05 28 views
0

嗨我有以下代碼片段用於將文件從.xlsm轉換爲.xlsx。代碼是在C#中,但我需要它在VB中。請諮詢。vb.net轉換

byte[] byteArray = File.ReadAllBytes("C:\\temp\\test.xlsm"); 
using (MemoryStream stream = new MemoryStream()) 
{ 
stream.Write(byteArray, 0, (int)byteArray.Length); 
using (SpreadsheetDocument spreadsheetDoc = SpreadsheetDocument.Open(stream, true)) 
// Change from template type to workbook type 
{ 
spreadsheetDoc.ChangeDocumentType (SpreadsheetDocumentType.Workbook); 
} 
File.WriteAllBytes ("C:\\temp\\test.xlsx", stream.ToArray()); 
} 

回答

1

你甚至試過自己做過嗎?有對那種可以轉換的幾個很好的在線工具,像一個:http://www.developerfusion.com/tools/convert/csharp-to-vb/

Dim byteArray As Byte() = File.ReadAllBytes("C:\temp\test.xlsm") 
Using stream As New MemoryStream() 
    stream.Write(byteArray, 0, CInt(byteArray.Length)) 
    Using spreadsheetDoc As SpreadsheetDocument = SpreadsheetDocument.Open(stream, True) 
     ' Change from template type to workbook type 
     spreadsheetDoc.ChangeDocumentType(SpreadsheetDocumentType.Workbook) 
    End Using 
    File.WriteAllBytes("C:\temp\test.xlsx", stream.ToArray()) 
End Using 
+0

+1在線轉換工具有其侷限性(lambda表達式,AddHandler的),但看起來不錯。 – MarkJ

+0

是的!試過。得到相同的轉換後的代碼。但是,當我保存工作簿啓用宏時,如下所示: spreadsheetDoc.ChangeDocumentType(SpreadsheetDocumentType.MacroEnabled工作簿)並保存,它在打開.xlsx文件時顯示錯誤,如無效文件格式。 你有什麼想法嗎? – chubhub