0
我試圖找出將multi dimensional C# list
插入excel sheet
並使用interop assembly
的方法我的列表中有多行數據分佈在8列上。但是,當我執行我的查詢扔我一個COM Exception from HRESULT: 0x800A03EC
,我有以下代碼將多維列表插入到Excel表中時發生COM異常
public void ExportStructureListToExcel(List<StructuresDS> listExport, string sheetName)
{
try
{
Microsoft.Office.Interop.Excel._Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel._Workbook workbook = app.Workbooks.Add(Type.Missing);
Microsoft.Office.Interop.Excel._Worksheet worksheet1 = null;
worksheet1 = (Microsoft.Office.Interop.Excel._Worksheet)workbook.Sheets["Sheet1"];
worksheet1 = (Microsoft.Office.Interop.Excel._Worksheet)workbook.ActiveSheet;
for (int i = 1; i < listExport.Count + 1; i++)
{
for (int j = 1; j < 8; j++)
{
worksheet1.Cells[i, j] = listExport[i - 1];
}
}
string fileDestination = @"S:\Parser Project\sde.xls";
if (File.Exists(fileDestination))
{
File.Delete(fileDestination);
}
workbook.SaveAs(fileDestination, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, Type.Missing, Type.Missing, Type.Missing, Type.Missing);
workbook.Close(true, Type.Missing, Type.Missing);
Process.Start(fileDestination);
app.Quit();
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
StructureDS結構
public class StructuresDS
{
public DateTime time;
public string CC;
public string term;
public string strike;
public string strategy;
public double? premium;
public int volume;
public double ratio;
public string over;
}
**Inserting elements to the List**
listStructures.Add(new StructuresDS
{
time = Convert.ToDateTime(AxiomSubSet[0].time.ToString("HH:mm:ss")),
CC = AxiomSubSet[0].CC,
term = listCodedTerms[0],
strike = (Convert.ToDouble(AxiomSubSet[0].strike) * 100).ToString(),
strategy = AxiomSubSet[0].strategy,
premium = Convert.ToDouble(AxiomSubSet[0].price),
volume = Convert.ToInt32(AxiomSubSet[0].quantity)
});
在worksheet1.Cells[i, j] = listExport[i - 1];
被拋出的錯誤,我無法找到一個解決這個問題。我可以知道我錯在哪裏嗎?
你還沒有說過哪一行會拋出錯誤。 – user845279 2014-09-02 18:36:22
它在worksheet1.Cells [i,j] = listExport [i - 1],我編輯了問題 – DoIt 2014-09-02 18:57:33
我認爲你需要'worksheet1.Cells [i,j] .Value = listExport [i - 1];'另外,我不知道StructureDS是什麼,但是您可能需要一個屬性,而不是結構本身。 – 2014-09-02 18:59:58