操作EXCEL
在我的winfrom里
public static void exporttoexce2(string filepath, DataSet ds, string SheetName, string Type)
{
Microsoft.Office.Interop.Excel.ApplicationClass myApp = null;
Microsoft.Office.Interop.Excel.Workbook myBook = null;
Microsoft.Office.Interop.Excel.Worksheet mySheet = null;
object oMissiong = System.Reflection.Missing.Value;
if (SheetName == "QUOTA" && Type == "DP")
{
System.Text.StringBuilder stringBuffer = new System.Text.StringBuilder();
for (int i = 0; i < ds.Tables.Count; i++)
{
myApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
myApp.Visible = false;
myApp.Workbooks.Open(filepath, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong);
System.Threading.Thread.Sleep(2000);
myBook = myApp.Workbooks[1];
mySheet = (Microsoft.Office.Interop.Excel.Worksheet)myBook.Sheets["ASQ"];
for (int j = 0; j < ds.Tables[i].Rows.Count; j++)
{
for (int k = 0; k < ds.Tables[i].Columns.Count; k++)
{
stringBuffer.Append((string)ds.Tables[i].Rows[j][k]);
if (k < ds.Tables[i].Columns.Count - 1)
stringBuffer.Append("/t");
}
stringBuffer.Append("/n");
}
}
System.Windows.Forms.Clipboard.SetDataObject("");
System.Windows.Forms.Clipboard.SetDataObject(stringBuffer);
[color=#FFFF00] ((Excel.Range)mySheet.Cells[2, 1]).Select();
// Range range = (Range)mySheet.Cells[2, 1];
// object c = range.Select();
mySheet.Paste(oMissiong, oMissiong); [/color]System.Windows.Forms.Clipboard.SetDataObject("");
myBook.Close(Excel.XlSaveAction.xlSaveChanges, oMissiong, oMissiong);
System.Runtime.InteropServices.Marshal.ReleaseComObject(myBook);
myBook = null;
myApp.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(myApp);
GC.Collect();
}
}
这个方法在调用的时候,蓝色部分有错误,我对这个不了解,有知道的 告诉我 该怎么写?