using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace SealerDataToExcel
{
public partial class form_main : Form
{
string filename;
string selectPath;
//数据库连接
SQLiteConnection m_dbConnection;
public form_main()
{
InitializeComponent();
//允许跨线程操作UI
System.Windows.Forms.Control.CheckForIllegalCrossThreadCalls = false;
}
private void btn_slelct_file_Click(object sender, EventArgs e)
{
DialogResult dr = openFileDialog1.ShowDialog();
//获取所打开文件的文件名
filename = openFileDialog1.FileName;
if (dr == System.Windows.Forms.DialogResult.OK && !string.IsNullOrEmpty(filename))
{
label_select.Text = "文件路径:" + filename;
}
}
private void btn_export_Click(object sender, EventArgs e)
{
//保存路径
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "/Excel 工作簿 (*.xls)|*.xls";
//string strName = null;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
selectPath = saveFileDialog.FileName;
label_export.Text = "导出路径:" + selectPath;
}
}
private void btn_export_excel_Click(object sender, EventArgs e)
{
ThreadStart ts = new ThreadStart(PrintEven2);
Thread t = new Thread(ts);
t.Start();
}
private void PrintEven()
{
if (filename != null && selectPath != null)
{
try
{
btn_export.Enabled = false;
btn_slelct_file.Enabled = false;
btn_export_excel.Enabled = false;
lable_process.Text = "链接数据库...";
//导出数据
SQLiteConnection.CreateFile("MyDatabase.sqlite");
m_dbConnection = new SQLiteConnection("Data Source=" + filename + ";Version=3;");
m_dbConnection.Open();
string sql = "select * from print_log";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
SQLiteDataReader reader = command.ExecuteReader();
lable_process.Text = "链接成功!";
lable_process.Text = "创建数据集...";
string[] FiveHeader = { "ID", "封口时间", "灭菌日期", "失效日期", "灭菌批次", "灭菌器号", "操作人员", "科室名称", "物品名称", "自定义内容", "温度", "压力", "打印序号" };
DataTable dataTable = new DataTable("ExportData");//*新建一张表
foreach (string TemStr in FiveHeader)
{
DataColumn strNameColumn = new DataColumn();
strNameColumn.DataType = typeof(String);
strNameColumn.ColumnName = TemStr;
dataTable.Columns.Add(strNameColumn);
}
int count = 0;
while (reader.Read())
{
DataRow rowData = dataTable.NewRow(); //*建立行数据
rowData["ID"] = reader["_id"];
rowData["封口时间"] = reader["PT_DATE"] + "";
rowData["灭菌日期"] = reader["STERILIZATION_DATE"] + "";
rowData["失效日期"] = reader["NVALID_DATE"] + "";
rowData["灭菌批次"] = reader["STERILIZATION_BATCH"] + "";
rowData["灭菌器号"] = reader["STERILIZATION_DEVICE_NUMBER"] + "";
rowData["操作人员"] = reader["OPERATOR"] + "";
rowData["科室名称"] = reader["DEPARTMENT_NAME"] + "";
rowData["物品名称"] = reader["GOOD_NAME"] + "";
rowData["自定义内容"] = reader["CUSTOM_CONTENT"] + "";
rowData["温度"] = reader["TEMPERATURE"] + "";
rowData["压力"] = reader["PRESSURE"] + "";
rowData["打印序号"] = reader["NUMBER"] + "";
dataTable.Rows.Add(rowData);
count++;
}
Console.ReadLine();
MessageBox.Show("count:" + count);
progressBar1.Maximum = count + 2;
if (DataTableToExcel(dataTable))
{
lable_process.Text = "导出成功!";
MessageBox.Show("导出成功!");
btn_export.Enabled = true;
btn_slelct_file.Enabled = true;
btn_export_excel.Enabled = true;
}
}
catch (Exception exception)
{
btn_export.Enabled = true;
btn_slelct_file.Enabled = true;
btn_export_excel.Enabled = true;
MessageBox.Show(exception.Message);
}
}
else
{
MessageBox.Show("路径为空!");
}
}
public bool DataTableToExcel(DataTable dataTable)
{
lable_process.Text = "转换数据...";
int SheetCount = Convert.ToInt32(Math.Ceiling(Convert.ToDouble(dataTable.Rows.Count) / 65535));
bool result = false;
IWorkbook workbook = null;
FileStream fs = null;
int count = 0;
try
{
if (dataTable != null && dataTable.Rows.Count > 0)
{
workbook = new HSSFWorkbook();
for (int k = 0; k < SheetCount; k++)
{
ISheet sheet = workbook.CreateSheet("Sheet" + k);
int rowCount = dataTable.Rows.Count;
int columnCount = dataTable.Columns.Count;
//设置列头
IRow row = sheet.CreateRow(0);
for (int i = 0; i < columnCount; i++)
{
ICell cell = row.CreateCell(i);
cell.SetCellValue(dataTable.Columns[i].ColumnName);
}
int range = 0;
if (k == SheetCount - 1)
{
range = rowCount;
}
else
{
range = (k + 1) * 65535;
}
for (int i = k * 65535, l = 1; i < range; i++, l++)
{
row = sheet.CreateRow(l);
if (i <= rowCount)
{
for (int j = 0; j < columnCount; j++)
{
ICell cell = row.CreateCell(j);
cell.SetCellValue(dataTable.Rows[i][j].ToString());
if(count< rowCount)
count++;
progressBar1.Value = count;
//able_process.Text = "数据集数据数量:" + count;
}
}
}
}
lable_process.Text = "写入到excel...";
using (fs = File.OpenWrite(selectPath))
{
workbook.Write(fs);
result = true;
}
}
return result;
}
catch (Exception ex)
{
MessageBox.Show(ex + "");
if (fs != null)
{
fs.Close();
}
return false;
}
}
private void PrintEven2()
{
if (filename != null && selectPath != null)
{
try
{
btn_export.Enabled = false;
btn_slelct_file.Enabled = false;
btn_export_excel.Enabled = false;
lable_process.Text = "链接数据库...";
//导出数据
SQLiteConnection.CreateFile("MyDatabase.sqlite");
m_dbConnection = new SQLiteConnection("Data Source=" + filename + ";Version=3;");
m_dbConnection.Open();
string sql = "select * from print_log";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
SQLiteDataReader reader = command.ExecuteReader();
lable_process.Text = "链接成功!";
lable_process.Text = "创建数据集...";
string[] FiveHeader = { "ID", "封口时间", "灭菌日期", "失效日期", "灭菌批次", "灭菌器号", "操作人员", "科室名称", "物品名称", "自定义内容", "温度", "压力", "打印序号" };
DataTable dataTable = new DataTable("ExportData");//*新建一张表
foreach (string TemStr in FiveHeader)
{
DataColumn strNameColumn = new DataColumn();
strNameColumn.DataType = typeof(String);
strNameColumn.ColumnName = TemStr;
dataTable.Columns.Add(strNameColumn);
}
int count = 0;
while (reader.Read())
{
DataRow rowData = dataTable.NewRow(); //*建立行数据
rowData["ID"] = reader["_id"];
rowData["封口时间"] = reader["PT_DATE"] + "";
rowData["灭菌日期"] = reader["STERILIZATION_DATE"] + "";
rowData["失效日期"] = reader["NVALID_DATE"] + "";
rowData["灭菌批次"] = reader["STERILIZATION_BATCH"] + "";
rowData["灭菌器号"] = reader["STERILIZATION_DEVICE_NUMBER"] + "";
rowData["操作人员"] = reader["OPERATOR"] + "";
rowData["科室名称"] = reader["DEPARTMENT_NAME"] + "";
rowData["物品名称"] = reader["GOOD_NAME"] + "";
rowData["自定义内容"] = reader["CUSTOM_CONTENT"] + "";
rowData["温度"] = reader["TEMPERATURE"] + "";
rowData["压力"] = reader["PRESSURE"] + "";
rowData["打印序号"] = reader["NUMBER"] + "";
dataTable.Rows.Add(rowData);
count++;
}
Console.ReadLine();
lable_process.Text = "数据集创建成功!";
//MessageBox.Show("count:" + count);
progressBar1.Maximum = count + 2;
if (DataTableToExcel(dataTable))
{
lable_process.Text = "";
MessageBox.Show("导出成功!");
progressBar1.Value = 0;
filename = "";
selectPath = "";
label_export.Text = "";
label_select.Text = "";
btn_export.Enabled = true;
btn_slelct_file.Enabled = true;
btn_export_excel.Enabled = true;
}
}
catch (Exception exception)
{
btn_export.Enabled = true;
btn_slelct_file.Enabled = true;
btn_export_excel.Enabled = true;
MessageBox.Show(exception.Message);
}
}
else
{
MessageBox.Show("路径为空!");
}
}
}
}
C# SqLIte数据导出到excel Demo
最新推荐文章于 2023-02-09 21:54:46 发布