C# winform应用 将数据库离得数据导出数据到word、excel、.txt汇总
using System;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using Excel;
using Word;
using System.IO;
namespace Common
{
/// <summary>
/// 把数据导入到.doc、.txt、.xls文件中
/// </summary>
public class Export
{
private const string DATAWORDPATH = @"C:\folder\doc\datadoc\";
private const string IMAGEWORDPATH = @"C:\folder\doc\imagedoc\";
private const string IMAGEPATH = @"C:\folder\image\";
private const string EXCELPATH = @"C:\folder\excel\";
private const string TXTPATH = @"C:\folder\txt\";
private const string IMAGEPOSTFIX = ".bmp";
private const string WORDPOSTFIX = ".doc";
private const string EXCELPOSTFIX = ".xls";
private const string TXTPOSTFIX = ".txt";
private const int DATADISTANCE = 5;
private const int TABDISTANCE = 8;
public Export()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
/// <summary>
/// 获得数据集Dataset--------------------------------用于调试
/// </summary>
/// <returns>Dataset</returns>
public DataSet GetData()
{
try
{
string sConnectionString;
sConnectionString = "数据库连接字 包括用户名,密码";
SqlConnection objConn = new SqlConnection(sConnectionString);
objConn.Open();
SqlDataAdapter daPoint = new SqlDataAdapter("Select * From Point", objConn);
DataSet dsYC = new DataSet("YC");
daPoint.FillSchema(dsYC,SchemaType.Mapped, "Point");
daPoint.Fill(dsYC,"Point");
daPoint = new SqlDataAdapter("Select * From Employee", objConn);
daPoint.FillSchema(dsYC,SchemaType.Mapped, "Employee");
daPoint.Fill(dsYC,"Employee");
return dsYC;
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
2楼
/// <summary>
/// 把数据文件导入到.xls文件
/// </summary>
/// <param name="ds"></param>
public void ExportToExcel(DataSet ds)
{
if(ds.Tables.Count!=0)
{
//生成.xls文件完整路径名
string tempFileName = GetTempFileName();
object filename = EXCELPATH+tempFileName+EXCELPOSTFIX;
object Nothing = System.Reflection.Missing.Value;
//创建excel文件,文件名用系统时间生成精确到毫秒
Excel.Application myExcel = new Excel.ApplicationClass();
myExcel.Application.Workbooks.Add(Nothing);
try
{
//把Dataset中的数据插入excel文件中
int totalCount = 0;
for(int k =0;k<ds.Tables.Count;k++)
{
int row = ds.Tables[k].Rows.Count;
int column = ds.Tables[k].Columns.Count;
for(int i = 0;i<column;i++)
{
myExcel.Cells[totalCount+2,1+i] = ds.Tables[k].Columns[i].ColumnName;
}
for(int i = 0;i<row;i++)
{
for(int j =0;j<column;j++)
{
myExcel.Cells[totalCount+3+i,1+j] = "'" + ds.Tables[k].Rows[i][j].ToString();
}
}
totalCount = totalCount + row +4;
}
try
{
//保存excel文件到指定的目录下,文件名用系统时间生成精确到毫
C#数据导出到Excel(或Word)源代码大全_IT/计算机_专业资料。数据导出到Excel(或Word)源代码大全,应有尽有,测试通过C#数据导出到Excel(或Word)源代码大全 在日常...
从数据库导出数据到word、excel、.txt_IT/计算机_专业资料。从数据库导出数据到... 【C#+winform】从数据库... 5页 1下载券 数据库导入导出命令 12页 免费 ...
C#将数据导出到 Excel表_IT/计算机_专业资料。C#...同理,我们也可 以输出(导出)图片、word 文档等。...的方法: 二、winForm 中导出 Execl 的方法: 1、...
搜 试试 7 帮助 全部 DOC PPT TXT PDF XLS ... C#将数据集DataSet中的数据导出到EXCEL文件的几种...(int c = 0; c < columnNumber; c++) { ...
?C# 连接sql数据库对 access,excel导入导出操作 一、SQL SERVER 和ACCESS的数据导入导出 常规的数据导入导出: 使用DTS向导迁移你的Access数据到SQL Server,你可以...
格式:txt 关键词:将导出导出数据 1/2 相关文档...Excel(datagrid1); 3,将 DataSet 中的数据导出 ...的方法: 二,winForm 中导出 Execl 的方法: 1,...
C#快速导出数据到excel_计算机软件及应用_IT/计算机...创建一个名为 C:\ExcelData 的新文件夹。示例程序...( m_strSampleFolder + "Book6.txt", System.IO....
C# Excel 导入到 Access数据库表_计算机软件及应用_IT/计算机_专业资料。C# Excel 导入到 Access数据库表(winForm版) 直接贴代码吧。 /// /// 获取Excel文...
());//数据库添加操作 } odr.Close(); } } return true; } catch (Exception) { return false; } } C#winform 从数据集导出 Excel(带指定 Excel 样式)...
Winform导入导出EXCEL_计算机软件及应用_IT/计算机_专业资料。Winform导入导出...(txtFilesUrl.Text.Length == 0) { MessageBoxEx.Show("请选择导入数据的...
热门文档