请大家帮忙!c#中的DataGrid数据导出excel中,如何加上excel标题?在线

解决方案 »

  1.   

    public static void DataTabletoExcel(System.Data.DataTable tmpDataTable,string strFileName)
      {
       if (tmpDataTable == null)
        return;
       int rowNum = tmpDataTable.Rows.Count;
       int columnNum = tmpDataTable.Columns.Count;
       int rowIndex = 1;
       int columnIndex = 0;   Excel.Application xlApp = new Excel.ApplicationClass();
       xlApp.DefaultFilePath = "";
       xlApp.DisplayAlerts = true;
       xlApp.SheetsInNewWorkbook = 1;
       Excel.Workbook xlBook = xlApp.Workbooks.Add(true);   //将DataTable的列名导入Excel表第一行
       foreach(DataColumn dc in tmpDataTable.Columns)
       {
        columnIndex ++;
        xlApp.Cells[rowIndex,columnIndex] = dc.ColumnName;
       }   //将DataTable中的数据导入Excel中
       for(int i = 0;i<rowNum; i++)
       {
        rowIndex ++;
        columnIndex = 0;
        for (int j = 0;j<columnNum; j++)
        {
         columnIndex ++;
         xlApp.Cells[rowIndex,columnIndex] = tmpDataTable.Rows[i][j].ToString();
        }
       }
       xlBook.SaveCopyAs(strFileName);
      }
      

  2.   

    我说的都是显示DataGrid数据,上面有一个标题