C#访问数据库进行查询并将查询结果导出到Excel的总结(窗体)
主要内容:1.C#访问数据库的语句(当前访问的是Access建的mdb数据库,后续转为Oracle建库)2.C#将查询结果导出到Excel3.在查询结果最后一行加入总计行,便于后期插入Word文档1.C#访问数据库的语句(当前访问的是Access建的mdb数据库,后续转为Oracle建库)加入using System.Data.OleDb;用于连接数据库using System.Data.OleD
主要内容:1.C#访问数据库的语句(当前访问的是Access建的mdb数据库,后续转为Oracle建库)
2.C#将查询结果导出到Excel
3.在查询结果最后一行加入总计行,便于后期插入Word文档
1.C#访问数据库的语句(当前访问的是Access建的mdb数据库,后续转为Oracle建库)
加入 using System.Data.OleDb;用于连接数据库
using System.Data.OleDb;
建立连接数据库的字符串 con,建立存储后续查询语句的字符串数组sql,用dbconn建立连接,用inst查询数据,用dt存储数据,将dt中的数据显示在窗体上的datagridview控件里,最后关闭连接
static string con = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Zhang\Desktop\Database1.mdb;Persist Security Info=False";//用于连接Access数据库的字符串
OleDbConnection dbconn = new OleDbConnection(con);
public static string[] sql = new string[16];
dbconn.Open();
OleDbDataAdapter inst = new OleDbDataAdapter(sql[choosenum], dbconn);
System.Data.DataTable dt = new System.Data.DataTable();
inst.Fill(dt);
dataGridView1.DataSource = dt;
dbconn.Close();
个人对于C#连接Oracle数据库的理解https://blog.csdn.net/linnge/article/details/108419781
2.C#将查询结果导出到Excel
加入 using Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;
一次导出多个表到一个Excel文件,不同表放在不同的Sheet上
int OLDOFFICEVESION = -4143;
int NEWOFFICEVESION = 56;
SaveFileDialog dialog = new SaveFileDialog();
dialog.Filter = "Excel file(*.xls)|*.xls";
dialog.FilterIndex = 0;
dialog.RestoreDirectory = true;
dialog.Title = "Export Excel File";
if (dialog.ShowDialog() == DialogResult.OK)
{
if (dialog.FileName == "")
{
MessageBox.Show("请输入保存文件名");
dialog.ShowDialog();
}
}
Microsoft.Office.Interop.Excel.Application xlApp1 = new ApplicationClass();
if (xlApp1 == null) { MessageBox.Show("xlApp为空,报错返回2"); }
Workbook xlBook1 = xlApp1.Workbooks.Add(true);
for (int i = 0; i < comboBox1.Items.Count; i++)
{
dbconn.Open();
OleDbDataAdapter inst = new OleDbDataAdapter(sql[i], dbconn);
System.Data.DataTable dt = new System.Data.DataTable();
inst.Fill(dt);
DataRow dr = dt.NewRow();
//MessageBox.Show(dt.Columns[0].GetType().ToString());
if (dt.Columns[0].DataType == typeof(string)) { dr[dt.Columns[0].ColumnName.ToString()] = "总计"; }
else { dr[dt.Columns[0].ColumnName.ToString()] = 111; }
for (int j = 1; j < dt.Columns.Count; j++)//增加最后合计行
{
if (dt.Columns[j].DataType == typeof(double))
{
double sumCount = 0;
string lie = dt.Columns[j].ColumnName.ToString();
object obj = dt.Compute("sum(" + lie + ")", "true");
if (obj != null && !string.IsNullOrEmpty(obj.ToString()))
{sumCount = (double)obj;}
dr[dt.Columns[j].ColumnName.ToString()] = sumCount;
}
else { dr[dt.Columns[j].ColumnName.ToString()] = "/"; }
}
dt.Rows.Add(dr);
dataGridView1.DataSource = dt;
dbconn.Close();
Worksheet xlSheet = (Worksheet)xlBook1.Worksheets[i + 1];
string ExcelTitle = comboBox1.Items[i].ToString().Split(new char[] { '(' })[0];
xlSheet.Name = ExcelTitle;
// Microsoft.Office.Interop.Excel.Worksheet sheet = (Worksheet)xlBook1.Sheets.Item[xlBook1.Sheets.Count];
xlBook1.Worksheets.Add(After: xlSheet);
int FormatNum;
string Version = xlApp1.Version;
if (Convert.ToDouble(Version) < 12)
{FormatNum = OLDOFFICEVESION;}
else { FormatNum = NEWOFFICEVESION; }
int ColIndex = 0, RowIndex = 0;
int ColCount = dataGridView1.ColumnCount, RowCount = dataGridView1.RowCount;
try
{
Range range = xlSheet.Range[xlSheet.Cells[1, 1], xlSheet.Cells[1, ColCount]];
range.MergeCells = true;
range.Value2 = comboBox1.Items[i].ToString();
range.Font.Size = 14;
range.Font.Bold = true;
range.HorizontalAlignment = Constants.xlCenter;
object[,] objData = new object[RowCount + 2, ColCount];//创建缓存数据
foreach (DataGridViewColumn col in dataGridView1.Columns) //获取列标题
{objData[RowIndex, ColIndex++] = col.HeaderText;}
//获取数据
for (RowIndex = 1; RowIndex < RowCount; RowIndex++)
{
for (ColIndex = 0; ColIndex < ColCount; ColIndex++)
{
if (dataGridView1[ColIndex, RowIndex - 1].ValueType == typeof(string) || dataGridView1[ColIndex, RowIndex - 1].ValueType == typeof(DateTime))
{
objData[RowIndex, ColIndex] = "";
if (dataGridView1[ColIndex, RowIndex - 1].Value != null)
{objData[RowIndex, ColIndex] = "" + dataGridView1[ColIndex, RowIndex - 1].Value.ToString();}
}
else
{objData[RowIndex, ColIndex] = dataGridView1[ColIndex, RowIndex - 1].Value;}
}
System.Windows.Forms.Application.DoEvents();
}
Range range1 = xlSheet.Range[xlSheet.Cells[2, 1], xlSheet.Cells[RowCount + 1, ColCount]];
range1.Value2 = objData;
xlSheet.Cells[RowCount + 1, 1] = "总计";
range1.HorizontalAlignment = 3;
range1.VerticalAlignment = 2;
xlBook1.Saved = true;
xlBook1.SaveAs(dialog.FileName, FormatNum);
}
catch (Exception err)
{ MessageBox.Show("Err:" + err.Message);}
}
xlApp1.Quit();
GC.Collect();//杀进程,避免软件结束后存在多个Microsoft Excel进程,进而影响Excel操作
3.在查询结果最后一行加入总计行,便于后期插入Word文档。先新建行dr,第一列字符填“总计”,判断列类型是字符型还是数字,”字符型填“/”,数字型求和
DataRow dr = dt.NewRow();
//MessageBox.Show(dt.Columns[0].GetType().ToString());
if (dt.Columns[0].DataType == typeof(string)) { dr[dt.Columns[0].ColumnName.ToString()] = "总计"; }
else { dr[dt.Columns[0].ColumnName.ToString()] = 111; }
for (int j = 1; j < dt.Columns.Count; j++)//增加最后合计行
{
if (dt.Columns[j].DataType == typeof(double))
{
double sumCount = 0;
string lie = dt.Columns[j].ColumnName.ToString();
object obj = dt.Compute("sum(" + lie + ")", "true");
if (obj != null && !string.IsNullOrEmpty(obj.ToString()))
{
sumCount = (double)obj;
}
dr[dt.Columns[j].ColumnName.ToString()] = sumCount;
}
else { dr[dt.Columns[j].ColumnName.ToString()] = "/"; }
}
dt.Rows.Add(dr);
}
更多推荐
所有评论(0)