当前位置:编程学习 > C#/ASP.NET >>

首先把datagridview查询出来,然后点导出,数据需要按照格式把导入至excel里边

如题
下边是图例

查处数据图

导出后需要完全按照这样的格式把数据显示出来 --------------------编程问答-------------------- 就是按模板导出:http://download.csdn.net/detail/flag5418/1573880 --------------------编程问答--------------------

 /*
                     * 获取保存EXCEL的路径
                     */
                    saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
                    saveFileDialog.FilterIndex = 0;
                    saveFileDialog.RestoreDirectory = true;
                    saveFileDialog.CreatePrompt = true;
                    saveFileDialog.Title = "导出文件保存路径";
                    if (saveFileDialog.ShowDialog() == DialogResult.OK)
                    {
                        //strName储存保存EXCEL路径
                        string strName = saveFileDialog.FileName;
                        if (strName.Length != 0)
                        {
                            System.Reflection.Missing miss = System.Reflection.Missing.Value;
                            Microsoft.Office.Interop.Excel.ApplicationClass excel = new Microsoft.Office.Interop.Excel.ApplicationClass();
                            excel.Application.Workbooks.Add(true); ;
                            //若是true,则在导出的时候会显示EXcel界面。
                            excel.Visible = false;
                            if (excel == null)
                            {
                                MessageBox.Show("EXCEL无法启动!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                            }
                            Microsoft.Office.Interop.Excel.Workbooks books = (Microsoft.Office.Interop.Excel.Workbooks)excel.Workbooks;
                            Microsoft.Office.Interop.Excel.Workbook book = (Microsoft.Office.Interop.Excel.Workbook)(books.Add(miss));
                            Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)book.ActiveSheet;
                             //生成字段名称,逐条写,无效率
                            excel.Cells.Select();
                       
                            for (int i = 1; i < gridView.ColumnCount; i++)
                            {
                                excel.Cells[2, i + 1] = gridView.Columns[i].HeaderText.ToString();
                            }
                            //以下为填充数据关键代码,逐条写,无效率
                            for (int i = 0; i < gridView.RowCount; i++)
                            {
                                for (int j = 0; j < gridView.ColumnCount; j++)
                                {
                                    Range contentRange = excel.Cells[i + 3, j + 1] as Range;//获取单元格
                                    if (gridView[j, i].Value == typeof(string))
                                    {
                                        excel.Cells[i + 3, j + 1] = "" + gridView[i, j].Value.ToString();
                                    }
                                    else
                                    {
                                        excel.Cells[i + 3, j + 1] = gridView[j, i].Value.ToString();
                                    }
                                }

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

                            }

                             sheet.SaveAs(strName, miss, miss, miss, miss, miss, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, miss, miss, miss);
                            book.Close(false, miss, miss);
                            books.Close();
                            excel.Quit();
                            System.Runtime.InteropServices.Marshal.ReleaseComObject(sheet);
                            System.Runtime.InteropServices.Marshal.ReleaseComObject(book);
                            System.Runtime.InteropServices.Marshal.ReleaseComObject(books);
                            System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
                            GC.Collect();
                        }
                    }
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,