操作EXCEL

xuwenxiang888 2012-09-05 04:42:01
在我的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();
}
}


这个方法在调用的时候,蓝色部分有错误,我对这个不了解,有知道的 告诉我 该怎么写?
...全文
58 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

62,253

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧