无法将类型Microsoft.Office.Interop.Excel.ApplicationClass隐式转换成Excel.ApplicationClass
想用C#创建一个Excel文件,其名字是从TextBox接收的,然后定义Excel的格式,却出现了“无法将类型Microsoft.Office.Interop.Excel...
想用C#创建一个Excel文件,其名字是从TextBox接收的,然后定义Excel的格式,却出现了“无法将类型Microsoft.Office.Interop.Excel.ApplicationClass隐式转换成Excel.ApplicationClass”请问怎么解决
展开
1个回答
2013-06-27
展开全部
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = c:\\sample.xls;Extended Properties=Excel 8.0" ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
string strCom = " SELECT * FROM [Sheet1$] " ;
myConn.Open ( ) ;
file://打开数据链接,得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
file://创建一个 DataSet对象
myDataSet = new DataSet ( ) ;
file://得到自己的DataSet对象
myCommand.Fill ( myDataSet , "[Sheet1$]" ) ;
file://关闭此数据链接
myConn.Close ( ) ;然后读取Excel,using System.Reflection; // 引用这个才能使用Missing字段
using Excel; Excel.ApplicationClass excel = new Excel.ApplicationClass();
excel.Visible = true; //激活Excel
Workbook wBook = excel.Workbooks.Add(true);
// Worksheet wSheet = (Excel._Worksheet)wBook.ActiveSheet;
Worksheet wSheet = (Excel.Worksheet)wBook.ActiveSheet;
excel.Cells[3, 5] = "本公司电话: " + Phone;
excel.Cells[4, 5] = "本公司传真: " + Zhen;
excel.Cells[5, 5] = "联系人: " + ComName;
excel.Cells[4, 1] = "客户: " + CustomerName;
excel.Cells[5, 1] = "联系人: " + Associate;
excel.Cells[3, 8] = "户名:";
excel.Cells[3, 9] = AccountName;
excel.Cells[4, 8] = "开户行:";
excel.Cells[4, 9] = BranchName;
excel.Cells[5, 8] = "帐号:";
excel.Cells[5, 9] = "'" + AccountID;
//设置禁止弹出保存和覆盖的询问提示框
excel.DisplayAlerts = false;
excel.AlertBeforeOverwriting = false;
//保存工作薄
// wBook.Save();
//每次保存激活的表,这样才能多次操作保存不同的Excel表,默认保存位置是在”我的文档"
excel.Cells.Font.Size = 12;
excel.Cells.Font.Bold = false;
// Excel.Range m_objRange = m_objRange.get_Range(1, 3);
wSheet.get_Range(excel.Cells[1, 3], excel.Cells[1, 3]).Font.Size = 24;
wSheet.get_Range(excel.Cells[1, 3], excel.Cells[1, 3]).Font.Bold = true;
wSheet.get_Range(excel.Cells[3, 1], excel.Cells[3, 1]).Font.ColorIndex = 3;//此处设为红色,不能用Font.Color来设置颜色
// m_objRange.Cells.Font.Size = 24;
// m_objRange.Cells.Font.Bold = true; excel.ActiveWorkbook.SaveCopyAs(filename);
excel.Quit();
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
string strCom = " SELECT * FROM [Sheet1$] " ;
myConn.Open ( ) ;
file://打开数据链接,得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
file://创建一个 DataSet对象
myDataSet = new DataSet ( ) ;
file://得到自己的DataSet对象
myCommand.Fill ( myDataSet , "[Sheet1$]" ) ;
file://关闭此数据链接
myConn.Close ( ) ;然后读取Excel,using System.Reflection; // 引用这个才能使用Missing字段
using Excel; Excel.ApplicationClass excel = new Excel.ApplicationClass();
excel.Visible = true; //激活Excel
Workbook wBook = excel.Workbooks.Add(true);
// Worksheet wSheet = (Excel._Worksheet)wBook.ActiveSheet;
Worksheet wSheet = (Excel.Worksheet)wBook.ActiveSheet;
excel.Cells[3, 5] = "本公司电话: " + Phone;
excel.Cells[4, 5] = "本公司传真: " + Zhen;
excel.Cells[5, 5] = "联系人: " + ComName;
excel.Cells[4, 1] = "客户: " + CustomerName;
excel.Cells[5, 1] = "联系人: " + Associate;
excel.Cells[3, 8] = "户名:";
excel.Cells[3, 9] = AccountName;
excel.Cells[4, 8] = "开户行:";
excel.Cells[4, 9] = BranchName;
excel.Cells[5, 8] = "帐号:";
excel.Cells[5, 9] = "'" + AccountID;
//设置禁止弹出保存和覆盖的询问提示框
excel.DisplayAlerts = false;
excel.AlertBeforeOverwriting = false;
//保存工作薄
// wBook.Save();
//每次保存激活的表,这样才能多次操作保存不同的Excel表,默认保存位置是在”我的文档"
excel.Cells.Font.Size = 12;
excel.Cells.Font.Bold = false;
// Excel.Range m_objRange = m_objRange.get_Range(1, 3);
wSheet.get_Range(excel.Cells[1, 3], excel.Cells[1, 3]).Font.Size = 24;
wSheet.get_Range(excel.Cells[1, 3], excel.Cells[1, 3]).Font.Bold = true;
wSheet.get_Range(excel.Cells[3, 1], excel.Cells[3, 1]).Font.ColorIndex = 3;//此处设为红色,不能用Font.Color来设置颜色
// m_objRange.Cells.Font.Size = 24;
// m_objRange.Cells.Font.Bold = true; excel.ActiveWorkbook.SaveCopyAs(filename);
excel.Quit();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询