我要将SQL中数据导成EXCEL,哪位大侠给我段代码参考啊?谢谢了!

解决方案 »

  1.   

    一般放在DGV中这样比较好,再从DGV中取出下面是从DGV全部取出后保存
     string saveFileName = "";
                //bool fileSaved = false;
                SaveFileDialog saveDialog = new SaveFileDialog();
                saveDialog.DefaultExt = "xls";
                saveDialog.Filter = "Excel文件|*.xls";
                saveDialog.FileName = "成绩册" + DateTime.Now.ToShortDateString();
                saveDialog.ShowDialog();
                saveFileName = saveDialog.FileName;
                if (saveFileName.IndexOf(":") < 0) return; //被点了取消 
                Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
                if (xlApp == null)
                {
                    MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");
                    return;
                }            Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
                Microsoft.Office.Interop.Excel.Workbook workbook = workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
                Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Worksheets[1];//取得sheet1             //写入标题
                for (int i = 0; i < dataGridView1.ColumnCount; i++)
                {
                    worksheet.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
                }
                //写入数值
                for (int r = 1; r < dataGridView1.Rows.Count - 1; r++)
                {
                    for (int i = 0; i < dataGridView1.ColumnCount; i++)
                    {                    string str = dataGridView1.Rows[r].Cells[i].Value.ToString();
                        if (string.IsNullOrEmpty(str))
                        {
                            str = " ";
                        }                    worksheet.Cells[r + 1, i + 1] = str;
                    }
                    System.Windows.Forms.Application.DoEvents();
                }
                worksheet.Columns.EntireColumn.AutoFit();//列宽自适应            //if (Microsoft.Office.Interop.cmbxType.Text != "Notification")
                //{
                //    Excel.Range rg = worksheet.get_Range(worksheet.Cells[2, 2], worksheet.Cells[ds.Tables[0].Rows.Count + 1, 2]);
                //    rg.NumberFormat = "00000000";
                //}            if (saveFileName != "")
                {
                    try
                    {
                        workbook.Saved = true;
                        workbook.SaveCopyAs(saveFileName);
                        //fileSaved = true;
                    }
                    catch (Exception ex)
                    {
                        //fileSaved = false;
                        MessageBox.Show("导出文件时出错,文件可能正被打开!\n" + ex.Message);
                    }            }
                //else
                //{
                //    fileSaved = false;
                //}
                xlApp.Quit();
                GC.Collect();//强行销毁             MessageBox.Show("保存成功", "提示", MessageBoxButtons.OK);
      

  2.   

    再发个选择导出  你注意下表头
     DataTable dt = new DataTable();
                DataTable dt2 = new DataTable();
                for (int i = 0; i < dataGridView1.ColumnCount; i++)
                {
                    if (i == 0)
                    {
                        dt.Columns.Add("准考证号", typeof(System.String));
                        dt2.Columns.Add("准考证号", typeof(System.String));                }
                    else
                    {
                        dt.Columns.Add(dataGridView1.Columns[i].HeaderText, typeof(System.String));
                        dt2.Columns.Add(dataGridView1.Columns[i].HeaderText, typeof(System.String));
                    }
                }            /////这是打印分数条的格式
                DataRow dr;
                for (int r = 0; r < dataGridView1.SelectedRows.Count; r++)
                {
                    dr = dt.NewRow();
                    for (int i = 0; i < dataGridView1.ColumnCount; i++)
                    {
                        dr[i] = dataGridView1.SelectedRows[r].Cells[i].Value.ToString();
                    }
                    dt.Rows.Add(dr);
                }
                DataRow dr2;
                for (int i = dt.Rows.Count - 1; i >= 0; i--)
                {
                    dr2 = dt.NewRow();
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        string ss = dt.Rows[i][j].ToString();
                        dr2[j] = dt.Rows[i][j];                }
                    dt2.Rows.Add(dr2.ItemArray);//不加ItemArray报错
                }
                //放到G2中再放入EXCEL
               // MessageBox.Show(dataGridView1.CurrentCell.RowIndex.ToString());
                DataTable dts = new DataTable();
                Excel.Application excel = new Excel.Application();
                excel.Application.Workbooks.Add(true);
                ////  excel.Columns.EntireColumn.AutoFit();// 列宽自适应
              
                excel.Visible = true;
                //生成字段名称
                for (int i = 0; i < dataGridView1.ColumnCount; i++)
                {
                    excel.Cells[1, i + 1] = dataGridView1.Columns[i].HeaderText;
                }              for (int i = 0; i < dataGridView1.ColumnCount; i++)
                {
                    if (i == 0)
                    {
                        dts.Columns.Add("准考证号", typeof(System.String));                }
                    else
                    {
                        dts.Columns.Add(dataGridView1.Columns[i].HeaderText, typeof(System.String));
                    }
                }            for (int i = 0; i < dt2.Rows.Count; i++)
                {                for (int j = 0; j < dt2.Columns.Count; j++)
                    {
                        excel.Cells[2 + i, 1 + j] = dt2.Rows[i][j].ToString();
                    }
                }         excel.Columns.EntireColumn.AutoFit();//写在前面还有点问题 
      

  3.   

    http://blog.csdn.net/lsj_zrp/archive/2009/07/29/4390944.aspx