关闭

C#操作Excel

5156人阅读 评论(1) 收藏 举报

1、添加引用——Microsoft Excel 9.0 Object Library


Excel.ApplicationClass Myexcel=new Excel.ApplicationClass();
if(Myexcel==null)
{
MessageBox.Show("EXCEL无法启动!","信息提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
return;
}
Myexcel.Visible=true;
Myexcel.UserControl=true;
Excel.Workbooks Myworkbooks=Myexcel.Workbooks;
Excel.Workbook Myworkbook=Myworkbooks.Open(@"i:/N_订单表.xls",Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing,Type.Missing);
Excel.Worksheet Myworksheet=(Excel.Worksheet)Myworkbook.Worksheets[1];
Excel.Range MyRange=Myworksheet.get_Range("A1","N1");
int Rindex=dataset1.Tables["N_订单表"].Rows.Count;
int Cindex=dataset1.Tables["N_订单表"].Columns.Count;
Object [] MyHead=new Object[Cindex];
for(int i=0;i<Cindex;i++)
{
MyHead[i]=dataset1.Tables["N_订单表"].Columns[i].Caption;
}
MyRange.Value2=MyHead;
Object [,] EValue=new Object[Rindex,Cindex];
MyRange=MyRange.get_Range("A2","N11");
for(int i=0;i<Rindex;i++)
{

for(int j=0;j<Cindex;j++)
{
EValue[i,j]=dataset1.Tables["N_订单表"].Rows[i][j].ToString();
}
}
MyRange.Value2=EValue;
Myworkbook.Save();
Myworkbooks.Close();
Myexcel.Quit();
MessageBox.Show("EXCEL保存成功","完成");

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:57450次
    • 积分:1043
    • 等级:
    • 排名:千里之外
    • 原创:41篇
    • 转载:10篇
    • 译文:0篇
    • 评论:6条
    文章分类
    最新评论