我在装有VS.net的机器上,调试通过了一些操作Excel的类和文件,在VS.net中可以通过添加Excel相关的
Com组件就可以操作了,现在我要把编译好的文件移植到另外一台机器中去使用,这台机器只装有.net Framework,我要如何把Excel相关的组件放在什么地方才能使用呢,救命啊~~~
Com组件就可以操作了,现在我要把编译好的文件移植到另外一台机器中去使用,这台机器只装有.net Framework,我要如何把Excel相关的组件放在什么地方才能使用呢,救命啊~~~
我要把一个EXCEL文件的一部分数据读到datagrid中,我先把数据读到一个dataset中,写命令语句是:
SELECT *FROM[Sheet1$Sheet1.Cells[5,2],Sheet1.Cells[9,7])],运行时,说FROM语句有语法错误.
请问应该怎样写啊.
另外我还想请教:怎么样在程序里判断office的版本是2000还是2003啊.
楼上的,怎么借人家贴问问题,,呵呵,,应该是*FROM错误吧,*和FROM没有空格吧,加一个看!
using System.IO;
using System.Collections;
using System.Threading;
using Office = Microsoft.Office.Core;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Data;
using System.Data.OracleClient;namespace ExcelTest
{
public class VkExcel
{
private Excel.Application excelApp = null;
private Excel.Workbook excelWorkbook = null;
private Excel.Sheets excelSheets = null;
private Excel.Worksheet excelWorksheet = null; private static object vk_missing = System.Reflection.Missing.Value; private static object vk_visible = true;
private static object vk_false = false;
private static object vk_true = true; private bool vk_app_visible = false; private object vk_filename; #region OPEN WORKBOOK VARIABLES
private object vk_update_links = 0;
private object vk_read_only = vk_true;
private object vk_format = 1;
private object vk_password = vk_missing;
private object vk_write_res_password = vk_missing;
private object vk_ignore_read_only_recommend = vk_true;
private object vk_origin = vk_missing;
private object vk_delimiter = vk_missing;
private object vk_editable = vk_false;
private object vk_notify = vk_false;
private object vk_converter = vk_missing;
private object vk_add_to_mru = vk_false;
private object vk_local = vk_false;
private object vk_corrupt_load = vk_false;
#endregion #region CLOSE WORKBOOK VARIABLES
private object vk_save_changes = vk_false;
private object vk_route_workbook = vk_false; #endregion
public VkExcel()
{
this.startExcel();
} public VkExcel(bool visible,string path,string cityid,int amountmerchant)
{
OracleLinkClass db = new OracleLinkClass();
OracleConnection conn = db.con();
AmountFileClass afc = new AmountFileClass();
string [] merchantname; //商户类型数组
merchantname = new string[amountmerchant]; //数组赋值 string [] merchanttype; //商户类型代码
merchanttype = new string[amountmerchant]; //数组赋值
int exclecount = 5 + amountmerchant; //用户报表标题列 //从数据库中获得文件名称
db.sqlCommand = "select * from MERCHANTTYPEINFO WHERE CITYID = '"+cityid+"'";
OracleDataReader odr = db.DBCommand(conn);
for(int i=0; i<merchantname.Length;)
{
while(odr.Read())
{
merchantname[i] = odr.GetOracleString(2).Value;
merchanttype[i] = odr.GetOracleString(1).Value;
i++;
}
}
odr.Close();
//conn.Dispose(); this.vk_app_visible = visible;
this.startExcel();
OpenFile("c:\\4-5.xls","");
string datetime = afc.filedate();
int y = 0;
for(int x = 5; x<exclecount;x++)
{
this.excelApp.Cells[x,1]=merchantname[y];
this.excelApp.Cells[x,2]=afc.countgross(cityid,merchanttype[y],conn,datetime);
this.excelApp.Cells[x,3]=afc.countmoney(cityid,merchanttype[y],conn,datetime);
this.excelApp.Cells[x,4]=afc.Disfrockcountgross(cityid,merchanttype[y],conn,datetime);
this.excelApp.Cells[x,5]=afc.Disfrockcountmoney(cityid,merchanttype[y],conn,datetime);
this.excelApp.Cells[x,6]=afc.ysqcountgross(cityid,merchanttype[y],conn,datetime);
this.excelApp.Cells[x,7]=afc.ysqcountmoney(cityid,merchanttype[y],conn,datetime);
this.excelApp.Cells[x,8]=afc.ysqdisfrockcountgross(cityid,merchanttype[y],conn,datetime);
this.excelApp.Cells[x,9]=afc.ysdisfrockqcountmoney(cityid,merchanttype[y],conn,datetime);
y++;
} this.excelApp.Cells[exclecount+1,1]="报表生成时间:"+System.DateTime.Now.Year+"年"+System.DateTime.Now.Month+"月"+System.DateTime.Now.Day+"日";
this .excelApp.Cells[3,9]="数据统计日期:"+datetime; conn.Dispose();
excelWorkbook.SaveCopyAs(path);//最后文件保存地方
//CloseFile();
}
#region START EXCEL
private void startExcel()
{
try
{
if( this.excelApp == null )
{
this.excelApp = new Excel.ApplicationClass();
}
// Make Excel Visible
this.excelApp.Visible = this.vk_app_visible;
}
catch(Exception ex)
{
Console.WriteLine(ex.Message+ex.StackTrace); }
}
#endregion #region STOP EXCEL
public void stopExcel()
{
if( this.excelApp != null )
{
Process[] pProcess;
pProcess = System.Diagnostics.Process.GetProcessesByName("Excel");
pProcess[0].Kill();
}
}
#endregion #region OPEN FILE FOR EXCEL
public string OpenFile(string fileName, string password)
{
vk_filename = fileName; if( password.Length > 0 )
{
vk_password = password;
} try
{
// Open a workbook in Excel
this.excelWorkbook = this.excelApp.Workbooks.Open(
fileName, vk_update_links, vk_read_only, vk_format, vk_password,
vk_write_res_password, vk_ignore_read_only_recommend, vk_origin,
vk_delimiter, vk_editable, vk_notify, vk_converter, vk_add_to_mru,
vk_local, vk_corrupt_load);
//excelWorkbook.Save();
}
catch(Exception e)
{
this.CloseFile();
return e.Message;
}
return "OK";
}
#endregion public void CloseFile()
{
excelWorkbook.Close(vk_save_changes,vk_filename,vk_route_workbook);
}
#region GET EXCEL SHEETS
public void GetExcelSheets()
{
if( this.excelWorkbook != null )
{
excelSheets = excelWorkbook.Worksheets;
}
}
#endregion #region FIND EXCEL ATP WORKSHEET
public bool FindExcelWorksheet(string worksheetName)
{
bool ATP_SHEET_FOUND = false; if( this.excelSheets != null )
{
// Step thru the worksheet collection and see if ATP sheet is
// available. If found return true;
for( int i=1; i<=this.excelSheets.Count; i++ )
{
this.excelWorksheet = (Excel.Worksheet)excelSheets.get_Item((object)i);
if( this.excelWorksheet.Name.Equals(worksheetName) )
{
this.excelWorksheet.Activate();
ATP_SHEET_FOUND = true;
return ATP_SHEET_FOUND;
}
}
}
return ATP_SHEET_FOUND;
}
#endregion #region GET RANGE
public string[] GetRange(string range)
{
Excel.Range workingRangeCells = excelWorksheet.get_Range(range,Type.Missing);
//workingRangeCells.Select();
System.Array array = (System.Array)workingRangeCells.Cells.Value2;
string[] arrayS = this.ConvertToStringArray(array); return arrayS;
}
#endregion #region CONVERT TO STRING ARRAY
private string[] ConvertToStringArray(System.Array values)
{
string[] newArray = new string[values.Length]; int index = 0;
for ( int i = values.GetLowerBound(0); i <= values.GetUpperBound(0); i++ )
{
for ( int j = values.GetLowerBound(1); j <= values.GetUpperBound(1); j++ )
{
if(values.GetValue(i,j)==null)
{
newArray[index]="";
}
else
{
newArray[index]=(string)values.GetValue(i,j).ToString();
}
index++;
}
}
return newArray;
}
#endregion
}
}
运行时,说FROM语句有语法错误.请问应该怎样写啊.
-----------------------------------------------SELECT * FROM [Sheet1$B5:G9]