dataset导出excel,带有多个sheet

public static void ToExcelSheet(DataSet ds, string fileName)
        {
            //int testnum = ds.Tables.Count-1;

            SaveFileDialog saveDialog = new SaveFileDialog();
            saveDialog.DefaultExt = "xls";
            saveDialog.Filter = "Excel文件|*.xls";
            saveDialog.FileName = fileName;
            saveDialog.ShowDialog();

            fileName = saveDialog.FileName;
            if (fileName.IndexOf(":") < 0) return; //被点了取消

            Microsoft.Office.Interop.Excel.Application appExcel;
            appExcel = new Microsoft.Office.Interop.Excel.Application();
            if (appExcel == null)
            {
                MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel");
                return;
            }

            Microsoft.Office.Interop.Excel.Workbook workbookData;
            Microsoft.Office.Interop.Excel.Worksheet worksheetData;
            Range range;
            workbookData = appExcel.Workbooks.Add(System.Reflection.Missing.Value);

            appExcel.DisplayAlerts = false;//不显示警告
            //xlApp.Visible = true;//excel是否可见
            //
            //for (int i = workbookData.Worksheets.Count; i > 0; i--)
            //{
            //    Microsoft.Office.Interop.Excel.Worksheet oWorksheet = (Microsoft.Office.Interop.Excel.Worksheet)workbookData.Worksheets.get_Item(i);
            //    oWorksheet.Select();
            //    oWorksheet.Delete();
            //}

            for (int k = 0; k < ds.Tables.Count; k++)
            {
                worksheetData = (Microsoft.Office.Interop.Excel.Worksheet)workbookData.Worksheets.Add(System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value);

                // testnum--;
                if (ds.Tables[k] != null)
                {
                    worksheetData.Name = ds.Tables[k].TableName;
                    //写入标题
                    for (int i = 0; i < ds.Tables[k].Columns.Count; i++)
                    {
                        worksheetData.Cells[1, i + 1] = ds.Tables[k].Columns[i].ColumnName;
                        range = (Range)worksheetData.Cells[1, i + 1];
                        range.Interior.ColorIndex = 15;
                        range.Font.Bold = true;
                        range.NumberFormatLocal = "@";//文本格式
                        // range.EntireColumn.AutoFit();//自动调整列宽
                        // range.WrapText = true; //文本自动换行  
                        range.ColumnWidth = 15;

                    }
                    //写入数值

                    for (int r = 0; r < ds.Tables[k].Rows.Count; r++)
                    {
                        for (int i = 0; i < ds.Tables[k].Columns.Count; i++)
                        {

                            worksheetData.Cells[r + 2, i + 1] = ds.Tables[k].Rows[r][i];
                            //Range myrange = worksheetData.get_Range(worksheetData.Cells[r + 2, i + 1], worksheetData.Cells[r + 3, i + 2]);

                            //myrange.NumberFormatLocal = "@";//文本格式
                            //// myrange.EntireColumn.AutoFit();//自动调整列宽
                            ////   myrange.WrapText = true; //文本自动换行  
                            //myrange.ColumnWidth = 15;

                        }
                        //  rowRead++;

                        System.Windows.Forms.Application.DoEvents();

                    }

                }

                worksheetData.Columns.EntireColumn.AutoFit();
                workbookData.Saved = true;

            }


            workbookData.SaveAs(fileName);
            workbookData.Close();

            appExcel.Quit();
            DialogResult btn = MessageBox.Show(null, "文件“" + fileName + "”保存成功。是否现在打开?", "信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

            if (btn == DialogResult.Yes)
            {
                ApplicationClass excelApp = new ApplicationClass();
                Workbook workbook2 = excelApp.Workbooks.Open(fileName);
                excelApp.Visible = true;

            }
            // appExcel.Quit();


            GC.Collect();
        }

调用方法:

        private void simpleButton1_Click(object sender, EventArgs e)
        {
           
            string str=@"Data Source=PC-20120629GXCM\BIAO;Initial Catalog=hotelbook;Integrated Security=True";
            SqlConnection con = new SqlConnection(str);
            SqlCommand cmd = new SqlCommand();
            string strcmd = "select * from 客房类型;" + "select * from 楼层信息;" + "select *  from 入住单;";
            cmd.CommandText = strcmd;
            con.Open();
            SqlDataAdapter sdr = new SqlDataAdapter(strcmd,con);
            DataSet ds = new DataSet();
            sdr.Fill(ds);
            ds.Tables[0].TableName = "客房类型";
            ds.Tables[1].TableName = "楼层类型";
            ds.Tables[2].TableName = "入住单";
            string fileName=null;
            //ToExcelSheet(ds, fileName);
            export.ToExcelSheet to = new ToExcelSheet();
            to.DataSet2Sheet(ds, fileName);
          
        }

posted @ 2012-08-21 17:20  郑松涛  阅读(199)  评论(0编辑  收藏  举报