65.9K
CodeProject 正在变化。 阅读更多。
Home

从数据库获取值并存储到 Excel 表格中

starIconemptyStarIconemptyStarIconemptyStarIconemptyStarIcon

1.00/5 (2投票s)

2007年12月18日

CPOL

3分钟阅读

viewsIcon

44662

downloadIcon

1405

从数据库获取值并存储到 Excel 表格中

引言

本文/代码片段的作用,为什么它有用,它解决了什么问题等等。

在这里,我发布了一个简单的程序,用于将值从数据库移动到 Excel。 它将存储在给定的路径中。

阅读我发布的示例,尝试实现它,当你遇到问题/错误时,发布你正在使用的代码,我会尽力帮助你,但是,不会直接发布你问题的解决方案,这样你就不必做任何事情,只需复制和粘贴即可使其运行

背景

要将数据导出到 Excel,我使用以下方法。 这种方法接受你的数据集(我假设你将其绑定到你的 DataGridView 或直接绑定),并用它填充一个 Excel 文件。 要使其工作,你需要添加对 Microsoft.Excel11 Interop 的引用(或 2007 的 12,XP 的 10 等)。 为此,请按照以下步骤操作

  1. 单击菜单上的 项目
  2. 从下拉菜单中选择 添加引用
  3. 对话框打开后,单击 COM 选项卡
  4. 向下滚动到 Microsoft Excel 12.0(或你正在运行的任何版本)并突出显示它
  5. 单击确定

使用代码

完成此操作后,将以下导入语句添加到你的文件中

            //Imports System.Runtime.InteropServices.Marshal
            //Imports Microsoft.Office.Interop.Excel  
                    

我为此有一个函数和一个子过程,一个用于实际创建 Excel 文件,一个用于将数据从 DataTable 转储到 Excel 文件中。 该函数,恰当地命名为 CreateExcelFile,接受 2 个参数,你希望保存文件的名称以及包含你数据的 DataTable。

代码

Imports System.Data.SqlClient

导入 System.Runtime.InteropServices.Marshal

导入 Microsoft.Office.Interop.Excel

‘ btn_Click 事件

Dim ds As New DataSet

Dim cmd As SqlCommand

Dim da As SqlDataAdapter

Dim dttest As DataTable

Dim consrt As String = "server=SERVER;database=AAAA;uid=sa; pwd=XXXX;"

Dim conn As New SqlConnection(consrt)

conn.Open()

cmd = New SqlCommand("select * from TableName ", conn)

cmd.CommandType = CommandType.Text

cmd.ExecuteNonQuery()

da = New SqlDataAdapter(cmd)

da.Fill(ds)

conn.Close()

Form1.CreateExcelFile(TextBox1.Text.Trim, ds)

End sub

共享函数

‘ 共享函数,用于在给定路径中创建和存储

Public Shared Function CreateExcelFile(ByVal fileName As String, ByVal ds1 As DataSet) As Boolean

Dim excelExport As New Microsoft.Office.Interop.Excel.Application()

Dim excelBook As Microsoft.Office.Interop.Excel.Workbook

Dim excelSheets As Microsoft.Office.Interop.Excel.Sheets

Dim excelSheet As Microsoft.Office.Interop.Excel.Worksheet

Dim excelCells As Microsoft.Office.Interop.Excel.Range

Dim location As Integer = System.Windows.Forms.Application.ExecutablePath.LastIndexOf("\")

Dim exPath As String = System.Windows.Forms.Application.ExecutablePath

Dim filePath As String

Dim dstemp As DataSet

dstemp = ds1

试试

filePath = exPath.Substring(0, (location + 1)) + "tmpFiles\" & fileName

If Not System.IO.Directory.Exists(exPath.Substring(0, (location + 1)) + "tmpFiles\") Then

System.IO.Directory.CreateDirectory(exPath.Substring(0, (location + 1)) + "tmpFiles\")

End If

excelExport.Visible = False : excelExport.DisplayAlerts = False

excelBook = excelExport.Workbooks.Add

excelSheets = excelBook.Worksheets

excelSheet = CType(excelSheets.Item(1), Microsoft.Office.Interop.Excel.Worksheet)

excelSheet.Name = "YourSheetName - " & Date.Now.Day & Date.Now.ToString("MMM") & Date.Now.ToString("yy")

excelCells = excelSheet.Cells

PopulateSheet(dstemp, excelCells)

excelSheet.SaveAs(filePath)

excelBook.Close()

excelExport.Quit()

ReleaseComObject(excelCells) : ReleaseComObject(excelSheet)

ReleaseComObject(excelSheets)

ReleaseComObject(excelBook) : ReleaseComObject(excelExport)

excelExport = Nothing : excelBook = Nothing : excelSheets = Nothing

excelSheet = Nothing : excelCells = Nothing

System.GC.Collect()

Return True

' End If

Catch ex As Exception

MsgBox(ex.Message, "导出数据时出错")

Return False

End Try

朋友们,请尽情享用,

这是我的第一篇文章,所以请接受它,如果其中有任何错误的话。

祝你有个愉快的一天

© . All rights reserved.