Excel文件是表结构的还是不规则的?
解决方案 »
- 类型映射
- 两个Picturebox叠加,如何做到上层半透明能看到下层图片
- [200分]征求磁盘之间文件快速复制方法或思路(C# WinForm)
- 请问C#中有没有树数据结构的类库
- 无法将类型“System.Data.DataView”隐式转换为“string”
- winform中如何重画TextBox为条线
- 用C#如何知道局域网中的一台服务器的数据库“服务管理器”是否开启(运行)?
- 大家在使用xtraGrid和DotNetBar的过程中,有没有发现bug?
- 如何将枚举变量转换成数组?
- webbrowser调试时,密码控件不可用
- 毕设卡壳了,救命啊:已有了Schema和WSDL,怎样在.net下开发Web Services
- C#作客户信息系统想分3层实现数据库和界面逻辑分离,老师帮助啊!
myExcel.Application.Workbooks.Open(filename,missing,missing,missing,missing,
missing,missing,missing,missing,missing,missing,missing,missing);
myExcel.Visible=false;
Excel.Worksheet worksheet=(Excel.Worksheet)myExcel.ActiveSheet;
string location=Server.MapPath("Data")+"\\testtext.txt";
FileStream OutStream=File.Create(location);
StreamWriter sw=new StreamWriter(OutStream);
string StringNum;
Session.Contents.Add("StringNum1","");
StringNum=worksheet.get_Range("c1","c1").Value2.ToString()+" ";
StringNum+=worksheet.get_Range("c2","c2").Value2.ToString()+" ";
StringNum+=worksheet.get_Range("e1","e1").Value2.ToString()+" ";
StringNum+=worksheet.get_Range("e2","e2").Value2.ToString()+" ";
myExcel.Application.Workbooks.Close();
worksheet=null;
myExcel.Application.Quit();
myExcel.Quit();
myExcel=null;
sw.WriteLine(StringNum);
sw.Flush();
sw.Close();
<IDENTITY IMPERSONATE="TURE"/>
missing,missing,missing,missing,missing,missing,missing,missing);
myExcel.Visible=false;
Excel.Worksheet worksheet=(Excel.Worksheet)myExcel.ActiveSheet;
string location=Server.MapPath("Data")+"\\testtext.txt";
FileStream OutStream=File.Create(location);
StreamWriter sw=new StreamWriter(OutStream);
string StringNum;
Session.Contents.Add("StringNum1","");
StringNum=worksheet.get_Range("c1","c1").Value2.ToString()+" ";
StringNum+=worksheet.get_Range("c2","c2").Value2.ToString()+" ";
StringNum+=worksheet.get_Range("e1","e1").Value2.ToString()+" ";
StringNum+=worksheet.get_Range("e2","e2").Value2.ToString()+" ";
myExcel.Application.Workbooks.Close();
worksheet=null;
myExcel.Application.Quit();
myExcel.Quit();
myExcel=null;
sw.WriteLine(StringNum);
sw.Flush();
sw.Close();
public static readonly string CONNECT_STRING = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=$datasource$;Extended Properties=Excel 8.0"; public static DataSet ExecuteSelectCommand(string cmdText,string conString, CommandType cmdType, params OleDbParameter[] cmdParms)
{
OleDbConnection conn = new OleDbConnection(conString);
OleDbCommand cmd = new OleDbCommand(cmdText,conn);
OleDbDataAdapter adpt = new OleDbDataAdapter(); cmd.CommandType = cmdType; if(null != cmdParms)
{
foreach(OleDbParameter para in cmdParms)
{
if(null == para.Value)
para.Value = DBNull.Value;
cmd.Parameters.Add(para);
}
} adpt.SelectCommand = cmd; try
{
DataSet dataSet = new DataSet();
adpt.Fill(dataSet);
return dataSet;
}
catch
{
throw;
}
finally
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
}其中$datasource$是Excel文件的全路径名,sql查询语句(cmdText)中的表名就是sheet的名字,但是要这样写"SELECT * FROM [sheet1$]",其中sheet1就是sheet的名字。
Excel2000生成的文件适用,Excel2003没有测试过。
using System;
using System.Data;
using System.Reflection;
static void Main(string[] args)
{
Excel.Application xApp=new Excel.ApplicationClass();
xApp.Visible=true;
Excel.Workbook xBook=xApp.Workbooks._Open(@"D:\Sample.xls",
Missing.Value,Missing.Value,Missing.Value,Missing.Value
,Missing.Value,Missing.Value,Missing.Value,Missing.Value
,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
//xBook=xApp.Workbooks.Add(Missing.Value);
Excel.Worksheet xSheet=(Excel.Worksheet)xBook.Sheets[1];
//Excel.Worksheet xSheet=(Excel.Worksheet)xApp.ActiveSheet;
Excel.Range rng1=xSheet.get_Range("A1",Type.Missing);
Console.WriteLine(rng1.Value2);
Excel.Range rng2=(Excel.Range)xSheet.Cells[3,1];
Console.WriteLine(rng2.Value2);
Excel.Range rng3=xSheet.get_Range("C6",Missing.Value);
rng3.Value2="Hello";
rng3.Interior.ColorIndex=6; //xBook.SaveAs(@"D:\CData.xls",Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value //,Excel.XlSaveAsAccessMode.xlNoChange,Missing.Value,Missing.Value,Missing.Value,Missing.Value,Missing.Value);
xBook.Save(); xSheet=null;
//xBook.Close(Missing.Value,Missing.Value,Missing.Value);
xBook=null;
xApp.Quit();
xApp=null;
}