SQLServer数据库读取数据在Excel中显示
来源:互联网 发布:office mac下载 编辑:程序博客网 时间:2024/05/02 18:05
.在WinForm界面和SQLServer数据库进行连接读取使用using System.Data.SqlClient;命名空间下的SqlDataAdapter,SqlCommand,SqlReader等,但是当我们要向Excel中读取时需要使用ADODB命名空间下的方法得到DataResult直接复制将减少大量的循环写入,从而省去大量的时间,注意Connction关闭的时间,如果提前关闭则第二个方法将不能读取:
private ADODB.Connection conn = null;
private ADODB.Recordset rs = null;
public Recordset GetRecordSet()
{
String strConn = @"PROVIDER=SQLOLEDB;SERVER=localhost;DATABASE=ZHANG_DL";
conn = new ADODB.Connection();
conn.ConnectionString = strConn;
try
{
conn.Open(strConn, "sa", "", -1);//这个方法的各个参数自己设定,根据自己的设置
}
catch (Exception ee)
{
throw new Exception(ee.Message);
}
finally
{
Console.WriteLine(conn.State);
}
rs = new ADODB.Recordset();
String sql = "SELECT * FROM T_EMPLOYEE";//从数据库的Employee表格的内容进行读取
rs.Open(sql, conn, CursorTypeEnum.adOpenKeyset, LockTypeEnum.adLockBatchOptimistic, (int)ADODB.CommandTypeEnum.adCmdText);//查一下每个参数的意义
return rs;
}
public void ShowInExcel(ADODB.Recordset rs)
{
String path=@"C:\Users\Administrator\Documents\book2.xlsx";//这个路径名可以根据前面的openFileDialog的方法动态的设定读取
object missing = System.Reflection.Missing.Value;
System.Globalization.CultureInfo currentCI = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");
Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wb = null;
Microsoft.Office.Interop.Excel.Worksheet sheet = null;
Microsoft.Office.Interop.Excel.Range rg = null;
wb = app.Workbooks.Add(true);
try
{
sheet = (Microsoft.Office.Interop.Excel.Worksheet)wb.Sheets[1];
app.Application.DisplayAlerts = false;
app.Application.Visible = false;
sheet.Cells[1, 1] = "EMPLOYEE_ID";
sheet.Cells[1, 2] = "EMPLOYEE_NAME";
sheet.Cells[1, 3] = "EMPLOYEE_AGE";
sheet.Cells[1, 4] = "EMPLOYEE_PHONE_NUMBER";
sheet.Cells[1, 5] = "EMPLOYEE_AGE";
rg = sheet.get_Range(sheet.Cells[2, 1], sheet.Cells[10, 5]);
// rg.Select();
rg.CopyFromRecordset(rs, missing, missing);
sheet.Columns.EntireColumn.AutoFit();
wb.SaveAs(path,missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
wb.Close(false, missing, missing);
wb = null;
app.Quit();
app = null;
}
catch (Exception ee)
{
throw new Exception(ee.Message);
}
finally
{
rs.Close();
}
}
- SQLServer数据库读取数据在Excel中显示
- 将excel中数据显示在页面上,然后再导入到SqlServer数据库中
- 利用jxl包从sqlserver数据库中读取数据存到Excel中
- 在SQLSERVER中读取Oracle中的数据
- excel 2003 读取sqlserver 数据库表数据(图)
- sqlserver 读取 excel中的数据
- 从数据库中读取数据到Excel
- 在delphi中读取Excel数据
- c#读取SqlServer数据显示到textbox中
- 读取Excel文件中的数据,并显示在MSFlexGrid表格中
- 利用 SqlbulkCopy 快速导入 Excel 数据至 SQLServer 数据库中
- Excel中的数据导入到SqlServer数据库中
- 从数据库中读取数据时报错,sqlserver默认值问题
- 读取word文档中表格数据到sqlserver数据库
- 读取excel文件显示在gridview中数据.字符.数字混合时无法全部读取的问题!
- net读取excel中数据并显示到gridview中
- vb读取excel中的数据并在窗体上显示
- ASP.net 从数据库中读取html格式的数据并显示在页面中
- SQLSERVER SQL性能优化
- RTL级 门级
- mySQL字符串处理之一个字段包含多个ID的解决
- 计算机基础知识之位、字节、字符
- 会计档案管理制度
- SQLServer数据库读取数据在Excel中显示
- centos 6 64位服务器搭建svn服务器
- 取消Word2007中“word正在设置文档的格式”,防止文档结构图错乱
- 《Linux设备节点创建》用户空间ueventd创建设备节点规则
- java动态代理
- 周记
- 一万个舍不得
- ActiveX控件创建流程
- 真的只能这样了