WINFORM中怎样将Datatable中的数据直接导入到固定格式的Excel中?

解决方案 »

  1.   

    首先你先设定好Excel的格式,然后通过指定单元格来插入数据.
    先引用Excel 2003的 的dll
     Excel.ApplicationClass excel = null;
                Excel.Workbooks workbooks;
                Excel.Workbook workbook;
                Excel.Worksheet worksheet;
                Excel.Sheets worksheets;
     excel = new Excel.ApplicationClass();
                workbooks = excel.Workbooks;
                workbook = workbooks.Add(@"C:\aa.xls"); //模板文件
                worksheets = (Excel.Sheets)excel.Worksheets;
     worksheet = (Excel.Worksheet)worksheets.get_Item(i + 1);
                        worksheet.Activate();     //必须写,用来触发数据的焦点。
       foreach (DataRow dr in dset.Tables[i].Rows)
                        {
                            columnIndex = StartColumnIndex;
                           object[] execelobj = new object[dset.Tables[i].Columns.Count];
                            foreach (DataColumn dtcolumn in dset.Tables[i].Columns)
                           {
                               excel.Cells[StartRowIndex, columnIndex] = dr[dtcolumn.ColumnName].ToString();
                                columnIndex += 1;
                            }
                            StartRowIndex += 1;                    }
    大概参照一下吧
      

  2.   

    Execl文件本身可以作为OleDb数据源,操作起来就像SQL Server数据库一样。
    string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + savePath + ";Extended Properties=Excel 8.0;";
    OleDbConnection conn = new OleDbConnection(strConn);
    OleDbCommand cmd = new OleDbCommand();
    cmd.Connection = conn;cmd.Connection.Open();
    cmd.CommandText = "XXXXXXXXXXXXXXXXXXX";
    cmd.ExecuteNonQuery();
      

  3.   

    忘了说了,上面的savePath是excel文件的路径
      

  4.   

    Excel.ApplicationClass excel = null;
    Excel.Workbooks workbooks;
    Excel.Workbook workbook;
    Excel.Worksheet worksheet;
    Excel.Sheets worksheets;
    excel = new Excel.ApplicationClass();
    workbooks = excel.Workbooks;
    workbook = workbooks.Add(filePath1); //模板文件
    worksheets = (Excel.Sheets)excel.Worksheets;
    worksheet = (Excel.Worksheet)worksheets.get_Item(1);
    worksheet.Activate();     //必须写,用来触发数据的焦点。
    int row=5;

    for(int i=0;i<myDataSet.Tables["shengchengbiao"].Rows.Count;i++ )
    {  for(int j=0;j<21;j++)
       {      
    excel.Cells[row,j+2]=myDataSet.Tables["shengchengbiao"].Rows[i][j].ToString();
        }
    row++;
    }  
    workbook.Close(false,missing,missing);
    workbooks.Close();
    excel.Quit();
    //System.Runtime.InteropServices.Marshal.ReleaseComObject();
    System.Runtime.InteropServices.Marshal.ReleaseComObject(worksheet);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(workbooks);
    System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
    GC.Collect();
    怎么它不能直接就保存进去Excel呢?还需要弹出是否保存Excel表,点“是”后才保存。现在我的那个有固定格式的Excel存在固定的目录下,我希望什么都不要弹出就能直接把Datatable中数据库保存在Execel里?这个该怎么实现啊?在线等
      

  5.   

    use OleDB to export your data to excel file, 
    reference:
    http://www.codeproject.com/csharp/excel_using_oledb.asp
      

  6.   

    excel.Visible = false;
    excel.DisplayAlerts=false;
    加入这两句就可以不显示了