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

ASP.NET(C#)将Excel和SQL Server相互导入导出。望提供代码

ASP.NET页面获取Excel文件,然后可以选Excel对应的数据库列,然后点导入。数据就写入数据库。相仿,更具条件查出数据。点导出,就成创建一个新的Excel文件。望附代码。货发到我邮箱89286968@qq.com。
答案:public void ExportTOExcel()
        {
            if (dbgname.Rows.Count == 0)
            {
                MessageBox.Show("没有数据可供导出!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.Filter = "Execl files (*.xls)|*.xls";
                saveFileDialog.FilterIndex = 0;
                saveFileDialog.RestoreDirectory = true;
                saveFileDialog.CreatePrompt = true;
                saveFileDialog.Title = "导出文件保存路径";
                saveFileDialog.ShowDialog();
                string strName = saveFileDialog.FileName;
                if (strName.Length != 0)
                {
                    ToolStripProgressBar toolStripProgressBar1 = new ToolStripProgressBar();
                    toolStripProgressBar1.Visible = true;
                    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); ;
                    excel.Visible = false;//若是true,则在导出的时候会显示EXcel界面。
                    if (excel == null)
                    {
                        MessageBox.Show("EXCEL无法启动!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }
                    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;
                    sheet.Name = "test";
                     
                    //生成字段名称
                    for (int i = 0; i < dbgname.ColumnCount-8; i++)
                    {
                        excel.Cells[1, i + 1] = dbgname.Columns[i].HeaderText.ToString();
                    }
                    //填充数据
                    for (int i = 0; i < dbgname.RowCount - 1; i++)
                    {
                        for (int j = 0; j < dbgname.ColumnCount - 8; j++)
                        {
                            if (dbgname[j, i].Value == typeof(string))
                            {
                                excel.Cells[i + 2, j + 1] = "" + dbgname[i, j].Value.ToString();
                            }
                            else
                            {
                                excel.Cells[i + 2, j + 1] = dbgname[j, i].Value.ToString();
                            }
                        }
                        toolStripProgressBar1.Value += 100 / dbgname.RowCount;
                    }
                    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();
                    MessageBox.Show("数据已经成功导出到:" + saveFileDialog.FileName.ToString(), "导出完成", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    toolStripProgressBar1.Value = 0;
                    toolStripProgressBar1.Visible = false;
                }
            }
        }

上一个:asp.net中,购物车的实现过程中,从购物车中的TextBox中接收商品的数量
下一个:在asp中,ajax接受的是乱码怎么办,var b =xmlHttpRequest.responseText;

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,