日期:2014-05-18  浏览次数:20916 次

DataTable 导出Exexl 或者 dataGridView导出Excel
如题

DataTable 导出Exexl 或者 dataGridView导出Excel

------解决方案--------------------
C# code

       public void ExportExcel(DataSet ds)        //以DataSet- 导出Excel文件   
        {
            if (ds == null) return;
            Microsoft.office.Interop.Excel.Application xlApp = new Microsoft.office.Interop.Excel.Application();

            if (xlApp == null)
            {
                MessageBox.Show("无法创建Excel对象,可能您的机子未安装Excel", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                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
            Microsoft.office.Interop.Excel.Range range;
            long totalCount = ds.Tables[0].Rows.Count;

            long rowRead = 0;
            float percent = 0;

            //worksheet.Cells[1, 1] = "报表标题";

            //写入字段
            for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
            {
                worksheet.Cells[1, i + 1] = ds.Tables[0].Columns[i].ColumnName;
                range = (Microsoft.office.Interop.Excel.Range)worksheet.Cells[1, i + 1];
                range.Interior.ColorIndex = 15;
                range.Font.Bold = true;
            }
            //写入数值
            for (int r = 0; r < ds.Tables[0].Rows.Count; r++)
            {
                for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
                {
                    worksheet.Cells[r + 2, i + 1] = ds.Tables[0].Rows[r][i];
                }
                rowRead++;
                percent = ((float)(100 * rowRead)) / totalCount;
            }
            xlApp.Visible = true;
        }

------解决方案--------------------
C# code

private void button1_Click(object sender, EventArgs e) 
        { 
            SaveFileDialog saveFileDialog = new SaveFileDialog(); 
            saveFileDialog.Filter = "Execl  files  (*.xls)|*.xls"; 
            saveFileDialog.FilterIndex = 0; 
            saveFileDialog.RestoreDirectory = true; 
            saveFileDialog.CreatePrompt = true;  //提示是否创建(*****).xls文件 
            saveFileDialog.Title = "导出Excel文件到"; 

            // saveFileDialog.ShowDialog(); 
            if (saveFileDialog.ShowDialog() == DialogResult.OK) 
            { 
                Stream myStream; 
                myStream = saveFileDialog.OpenFile(); 
                StreamWriter sw = new StreamWriter(myStream, System.Text.Encoding.GetEncoding("gb2312")); 

                string str = ""; 
                try 
                { 
                    //写标题    
                    for (int i = 0; i < dataGridView1.ColumnCount; i++) 
                    { 
                        if (i > 0) 
                        { 
                            str += "\t"; 
                        } 
                        str += dataGridView1.Columns[i].HeaderText; 
                    } 

                    sw.WriteLine(str); 
                    //写内容  
                    for (int j = 0; j < dataGridView1.Rows.Count - 1; j++) 
                    { 
                        string tempStr = ""; 
                        for (int k = 0; k < dataGridView1.Columns.Count; k++) 
                        { 
                            if (k > 0) 
                            { 
                                tempStr += "\t"; 
                            } 
                            tempStr += dataGridView1.Rows[j].Cells[k].Value.ToString(); 
                        }