急!请问:如何在C#中连接EXCEL表?能不能给出例子? rt 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 using System;namespace PM.IO{ /// <summary> /// 本类提供相关数据导出到Excel的操作,用于完成报表打印和文件导出的功能。 /// </summary> public class Report { //private static Excel.Application excel ; //= new Excel.ApplicationClass(); /// <summary> /// 初始化类实例。 /// </summary> public Report() { }// /// <summary>// /// 取得Excel实例。// /// </summary>// private static Excel.Application ExcelInstance// {// get// {// if(excel == null )// excel = new Excel.ApplicationClass();//// return excel;// }// set// {// excel = null;// }// }// /// <summary>// /// 导出单个项目库项目,用于打印报表之用。// /// </summary>// /// <param name="id">项目ID。</param>// public static void ExportSingleProject(int id)// {// Information.Data.Access.Accessor accessor = Application.Instance.DbEnvironment.CreateAccessor();//// Information.Collections.NameObjectCollection c = (Information.Collections.NameObjectCollection)accessor.Query("select A.*, B.Name as AreaName from Projects A left join AreaCodes B on A.AreaCode = B.Code where A.ID=" + id,Information.Data.Access.ResultType.Dictionary);//// //Excel.Workbook book = ExcelInstance.Workbooks.Open("Reports\\Project.xls",new object(),new object(),new object(),new object(),new object(),new object(),new object(),new object(),new object(),new object(),new object(),new object(),new object(),new object());// Excel.Workbook book = ExcelInstance.Workbooks.Open(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) +"\\Reports\\Project.xls",null,null,null,null,null,null,null,null,null,null,null,null,null,null);//// Excel.Worksheet sheet = (Excel.Worksheet)book.Worksheets[0];//// if(System.IO.File.Exists(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\Cache\\_Project.xls"))// System.IO.File.Delete(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\Cache\\_Project.xls");//// //以下代码写入Excel文件项目内容。//// sheet.Cells[2,1] = "地区: "+c["AreaName"].ToString() + " "+"日期:" + DateTime.Now.ToLongDateString();//// sheet.Cells[3,2] = c["Name"].ToString();// sheet.Cells[4,2] = c["Kind"].ToString(); // sheet.Cells[5,2] = c["Owner"].ToString();// sheet.Cells[6,2] = c["Size"].ToString(); // sheet.Cells[7,2] = "总投资" + decimal.Parse(c["Invest"].ToString()).ToString("F2") + "万元,其中固定资产投资"+ decimal.Parse(c["Fixup"].ToString()).ToString("F2") + "万元,流动资金"+decimal.Parse(c["Dynamic"].ToString()).ToString("F2") + "万元。";// sheet.Cells[8,2] = "自筹" + decimal.Parse(c["Financing"].ToString()).ToString("F2") + "万元,贷款"+ decimal.Parse(c["Provide"].ToString()).ToString("F2") + "万元,引进"+decimal.Parse(c["Indraught"].ToString()).ToString("F2") + "万元,其他"+decimal.Parse(c["Other"].ToString()).ToString("F2") + "万元。";// sheet.Cells[9,2] = "产值"+decimal.Parse(c["Production"].ToString()).ToString("F2") + "万元,利税"+decimal.Parse(c["Revenue"].ToString()).ToString("F2") + "万元。";// sheet.Cells[10,2] = c["CooperateMode"].ToString();// sheet.Cells[11,2] = c["BuildCondition"].ToString();// sheet.Cells[12,2] = c["Address"].ToString();// sheet.Cells[13,2] = c["Linkman"].ToString();// sheet.Cells[14,2] = c["PostCode"].ToString();// sheet.Cells[15,2] = c["Tel"].ToString();// sheet.Cells[16,2] = c["Email"].ToString();// sheet.Cells[17,2] = c["Fax"].ToString();//// accessor = null;//// if(!book.Saved)// book.Saved = true;//// book.SaveAs("Cache\\_Project.xls",null,null,null,null,null,Excel.XlSaveAsAccessMode.xlShared,null,null,null,null,null);// // ExcelInstance.Visible = true;//// sheet.PrintOut(null,null,1,true,null,null,null,null);//// sheet = null;// book =null;// ExcelInstance.Quit();// } }} 看错了,你是想用EXCEL表做数据源么?放一个连接控件,然后手工设置其连接到EXCEL表;再从属性中把连接串复制下来就是了。 还是直接想,楼主用oleDb Jet4 string strconnection;strconnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\汇总表.xls;Extended Properties=Excel 8.0"";OleDbConnection connection = new OleDbConnection(strconnection);connection.Open(); 刚才是建立连接,接着是连接excle中的表:DataSet dataset = new DataSet();dataset.Tables.Add("excle1"); OleDbDataAdapter dataadapter1 = new OleDbDataAdapter("SELECT * FROM [sheet$]", connection1); "sheet"是表名 COM方式,得装Excelobject missing=Missing.Value;Excel.Application excel=new Excel.Application ( );excel.Application.Workbooks.Open(FileName,missing,missing,missing,missing,missing,missing,missing,missing,missing,missing, missing,missing, missing,missing); excel.Visible = false;for(int i = 0 ; i < excel.Worksheets.Count; i ++){MessageBox.Show(((Excel.Worksheet)excel.Worksheets[i + 1]).Name);}OleDb方式:string ConnString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0",path);OleDbConnection conn = new OleDbConnection(ConnString);conn.Open();DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[] {null, null, null, "TABLE"});conn.Close(); ***GoldPrinter.ExcelExpert(MIS金质打印通之Excel专版)***1、如何将各版本Excel(COM类型库) 转换生成.Net公共语言运行库程序集如Interop.Excel.dll2、如何引用Excel库或已生成的程序集3、创建Excel实例4、打开Excel工作薄工作表和模板5、增、删、改、插入、定位、重命名工作表6、获取活动工作表指定范围单元格及带区7、读、写单元格内容8、向带区中写内容9、单元格合并10、字体及颜色11、边框及网格线12、保存、另存13、打印、预览14、彻底关闭Excel进程15、综合应用,通打天下报表...需要的朋友:Email:[email protected] QQ:150439795更多源码:www.webmis.com.cn sos...关于c#... c#如何模拟键盘中的组合键? 请问owc11如何屏蔽右键 c# 有关路径的问题 没有堆栈如何"全部展开" Response.Write("<table border ='0'><tr align = 'center'>");怎么让他指定的表格里显示。 请问在C#中如何设置Excel某列的格式: 请教用C#或VB.net如何开发视频播放程序,如何对mpeg等格式进行编码? ASP.Net 类文件怎么上传到服务器? gridview 批量保存数据记录个数 请教XMLDocument的Load机制 伪静态传参问题
{
/// <summary>
/// 本类提供相关数据导出到Excel的操作,用于完成报表打印和文件导出的功能。
/// </summary>
public class Report
{
//private static Excel.Application excel ; //= new Excel.ApplicationClass(); /// <summary>
/// 初始化类实例。
/// </summary>
public Report()
{
}// /// <summary>
// /// 取得Excel实例。
// /// </summary>
// private static Excel.Application ExcelInstance
// {
// get
// {
// if(excel == null )
// excel = new Excel.ApplicationClass();
//
// return excel;
// }
// set
// {
// excel = null;
// }
// }// /// <summary>
// /// 导出单个项目库项目,用于打印报表之用。
// /// </summary>
// /// <param name="id">项目ID。</param>
// public static void ExportSingleProject(int id)
// {
// Information.Data.Access.Accessor accessor = Application.Instance.DbEnvironment.CreateAccessor();
//
// Information.Collections.NameObjectCollection c = (Information.Collections.NameObjectCollection)accessor.Query("select A.*, B.Name as AreaName from Projects A left join AreaCodes B on A.AreaCode = B.Code where A.ID=" + id,Information.Data.Access.ResultType.Dictionary);
//
// //Excel.Workbook book = ExcelInstance.Workbooks.Open("Reports\\Project.xls",new object(),new object(),new object(),new object(),new object(),new object(),new object(),new object(),new object(),new object(),new object(),new object(),new object(),new object());
// Excel.Workbook book = ExcelInstance.Workbooks.Open(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) +"\\Reports\\Project.xls",null,null,null,null,null,null,null,null,null,null,null,null,null,null);
//
// Excel.Worksheet sheet = (Excel.Worksheet)book.Worksheets[0];
//
// if(System.IO.File.Exists(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\Cache\\_Project.xls"))
// System.IO.File.Delete(System.IO.Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath) + "\\Cache\\_Project.xls");
//
// //以下代码写入Excel文件项目内容。
//
// sheet.Cells[2,1] = "地区: "+c["AreaName"].ToString() + " "+"日期:" + DateTime.Now.ToLongDateString();
//
// sheet.Cells[3,2] = c["Name"].ToString();
// sheet.Cells[4,2] = c["Kind"].ToString();
// sheet.Cells[5,2] = c["Owner"].ToString();
// sheet.Cells[6,2] = c["Size"].ToString();
// sheet.Cells[7,2] = "总投资" + decimal.Parse(c["Invest"].ToString()).ToString("F2") + "万元,其中固定资产投资"+ decimal.Parse(c["Fixup"].ToString()).ToString("F2") + "万元,流动资金"+decimal.Parse(c["Dynamic"].ToString()).ToString("F2") + "万元。";
// sheet.Cells[8,2] = "自筹" + decimal.Parse(c["Financing"].ToString()).ToString("F2") + "万元,贷款"+ decimal.Parse(c["Provide"].ToString()).ToString("F2") + "万元,引进"+decimal.Parse(c["Indraught"].ToString()).ToString("F2") + "万元,其他"+decimal.Parse(c["Other"].ToString()).ToString("F2") + "万元。";
// sheet.Cells[9,2] = "产值"+decimal.Parse(c["Production"].ToString()).ToString("F2") + "万元,利税"+decimal.Parse(c["Revenue"].ToString()).ToString("F2") + "万元。";
// sheet.Cells[10,2] = c["CooperateMode"].ToString();
// sheet.Cells[11,2] = c["BuildCondition"].ToString();
// sheet.Cells[12,2] = c["Address"].ToString();
// sheet.Cells[13,2] = c["Linkman"].ToString();
// sheet.Cells[14,2] = c["PostCode"].ToString();
// sheet.Cells[15,2] = c["Tel"].ToString();
// sheet.Cells[16,2] = c["Email"].ToString();
// sheet.Cells[17,2] = c["Fax"].ToString();
//
// accessor = null;
//
// if(!book.Saved)
// book.Saved = true;
//
// book.SaveAs("Cache\\_Project.xls",null,null,null,null,null,Excel.XlSaveAsAccessMode.xlShared,null,null,null,null,null);
//
// ExcelInstance.Visible = true;
//
// sheet.PrintOut(null,null,1,true,null,null,null,null);
//
// sheet = null;
// book =null;
// ExcelInstance.Quit();
// }
}
}
strconnection = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\汇总表.xls;Extended Properties=Excel 8.0"
";
OleDbConnection connection = new OleDbConnection(strconnection);
connection.Open();
DataSet dataset = new DataSet();
dataset.Tables.Add("excle1");
OleDbDataAdapter dataadapter1 = new OleDbDataAdapter("SELECT * FROM [sheet$]", connection1); "sheet"是表名
object missing=Missing.Value;
Excel.Application excel=new Excel.Application ( );
excel.Application.Workbooks.Open(FileName,missing,missing,missing,missing,
missing,missing,missing,missing,missing,missing, missing,missing, missing,missing);
excel.Visible = false;
for(int i = 0 ; i < excel.Worksheets.Count; i ++)
{
MessageBox.Show(((Excel.Worksheet)excel.Worksheets[i + 1]).Name);
}
OleDb方式:
string ConnString = string.Format(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties=Excel 8.0",path);
OleDbConnection conn = new OleDbConnection(ConnString);
conn.Open();
DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new object[] {null, null, null, "TABLE"});
conn.Close();
***GoldPrinter.ExcelExpert(MIS金质打印通之Excel专版)***
1、如何将各版本Excel(COM类型库) 转换生成.Net公共语言运行库程序集如Interop.Excel.dll
2、如何引用Excel库或已生成的程序集
3、创建Excel实例
4、打开Excel工作薄工作表和模板
5、增、删、改、插入、定位、重命名工作表
6、获取活动工作表指定范围单元格及带区
7、读、写单元格内容
8、向带区中写内容9、单元格合并
10、字体及颜色
11、边框及网格线
12、保存、另存
13、打印、预览
14、彻底关闭Excel进程
15、综合应用,通打天下报表
...需要的朋友:
Email:[email protected] QQ:150439795
更多源码:www.webmis.com.cn