C# WPF - 将 SQLite 数据库复制到 Excel
C# WPF - Copy SQLite Database to Excel
我目前运行一个C#项目,它处理大量数据并将其存储在SQLite数据库中。但是我想将这些文件从数据库导出到一个简单的 excel sheet,例如创建一些文件,其中数据库中的每个 table 都是 sheet excel, 只是一个普通的副本。
目前我正在用 .csv 流编写器做同样的事情,它非常慢,因为我有大约 140000 个数据集。这意味着需要将 table 作为一个整体复制或逐块复制。
我没有找到任何代码片段如何在 c# 中使用 sqlite 执行此操作。你有什么想法,我该怎么做?
我从来没有在 SQLite 中这样做过,我也认为直接输出总是更好,但我对此很好奇。
所以我写了这个试用版:
using System;
using System.Data.SQLite;
using Excel = Microsoft.Office.Interop.Excel;
namespace ExcelSqlite
{
internal class Program
{
private static void Main(string[] args)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
string cs = "URI=file:test.db";
string data = String.Empty;
int i = 0;
int j = 0;
using (SQLiteConnection con = new SQLiteConnection(cs))
{
con.Open();
string stm = "SELECT * FROM Contacts";
using (SQLiteCommand cmd = new SQLiteCommand(stm, con))
{
using (SQLiteDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read()) // Reading Rows
{
for (j = 0; j <= rdr.FieldCount - 1; j++) // Looping throw colums
{
data = rdr.GetValue(j).ToString();
xlWorkSheet.Cells[i + 1, j + 1] = data;
}
i++;
}
}
}
con.Close();
}
xlWorkBook.SaveAs("sqliteToExcel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
private static void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
}
finally
{
GC.Collect();
}
}
}
}
希望如此,这将引导您走向正确的方向。
我目前运行一个C#项目,它处理大量数据并将其存储在SQLite数据库中。但是我想将这些文件从数据库导出到一个简单的 excel sheet,例如创建一些文件,其中数据库中的每个 table 都是 sheet excel, 只是一个普通的副本。
目前我正在用 .csv 流编写器做同样的事情,它非常慢,因为我有大约 140000 个数据集。这意味着需要将 table 作为一个整体复制或逐块复制。
我没有找到任何代码片段如何在 c# 中使用 sqlite 执行此操作。你有什么想法,我该怎么做?
我从来没有在 SQLite 中这样做过,我也认为直接输出总是更好,但我对此很好奇。
所以我写了这个试用版:
using System;
using System.Data.SQLite;
using Excel = Microsoft.Office.Interop.Excel;
namespace ExcelSqlite
{
internal class Program
{
private static void Main(string[] args)
{
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
xlApp = new Excel.Application();
xlWorkBook = xlApp.Workbooks.Add(misValue);
xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
string cs = "URI=file:test.db";
string data = String.Empty;
int i = 0;
int j = 0;
using (SQLiteConnection con = new SQLiteConnection(cs))
{
con.Open();
string stm = "SELECT * FROM Contacts";
using (SQLiteCommand cmd = new SQLiteCommand(stm, con))
{
using (SQLiteDataReader rdr = cmd.ExecuteReader())
{
while (rdr.Read()) // Reading Rows
{
for (j = 0; j <= rdr.FieldCount - 1; j++) // Looping throw colums
{
data = rdr.GetValue(j).ToString();
xlWorkSheet.Cells[i + 1, j + 1] = data;
}
i++;
}
}
}
con.Close();
}
xlWorkBook.SaveAs("sqliteToExcel.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
xlWorkBook.Close(true, misValue, misValue);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
}
private static void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch (Exception ex)
{
obj = null;
}
finally
{
GC.Collect();
}
}
}
}
希望如此,这将引导您走向正确的方向。