把EXCEL文件当做一个数据源来进行数据的读取 *** 作,实例如下:
public DataSet ExcelToDS(string Path){
string strConn = "Provider=MicrosoftJetOLEDB40;" +"Data Source="+ Path +";"+"Extended Properties=Excel 80;";
OleDbConnection conn = new OleDbConnection(strConn);
connOpen();
string strExcel = "";
OleDbDataAdapter myCommand = null;
DataSet ds = null;
strExcel="select from [sheet1$]";
myCommand = new OleDbDataAdapter(strExcel, strConn);
ds = new DataSet();
myCommandFill(ds,"table1");
return ds;
}
对于EXCEL中的表即sheet([sheet1$])如果不是固定的可以使用下面的方法得到
string strConn = "Provider=MicrosoftJetOLEDB40;" +"Data Source="+ Path +";"+"Extended Properties=Excel 80;";OleDbConnection conn = new OleDbConnection(strConn);
DataTable schemaTable = objConnGetOleDbSchemaTable(SystemDataOleDbOleDbSchemaGuidTables,null);
string tableName=schemaTableRows[0][2]ToString()Trim();
另外:也可进行写入EXCEL文件,实例如下:
public void DSToExcel(string Path,DataSet oldds)
{
//先得到汇总EXCEL的DataSet 主要目的是获得EXCEL在DataSet中的结构
string strCon = " Provider = MicrosoftJetOLEDB40 ; Data Source ="+path1+";Extended Properties=Excel 80" ;
OleDbConnection myConn = new OleDbConnection(strCon) ;
string strCom="select from [Sheet1$]";
myConnOpen ( ) ;
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom, myConn ) ;
ystemDataOleDbOleDbCommandBuilder builder=new OleDbCommandBuilder(myCommand);
//QuotePrefix和QuoteSuffix主要是对builder生成InsertComment命令时使用。
builderQuotePrefix="["; //获取insert语句中保留字符(起始位置)
builderQuoteSuffix="]"; //获取insert语句中保留字符(结束位置)
DataSet newds=new DataSet();
myCommandFill(newds ,"Table1") ;
for(int i=0;i<olddsTables[0]RowsCount;i++)
{
在使用ImportRow后newds内有值,但不能更新到Excel中因为所有导入行的
DataRowState!=AddedDataRow nrow=aDataSetTables["Table1"]NewRow();
for(int j=0;j<newdsTables[0]ColumnsCount;j++)
{
nrow[j]=olddsTables[0]Rows[i][j];
}
newdsTables["Table1"]RowsAdd(nrow);
}
myCommandUpdate(newds,"Table1");
myConnClose();
}
2方法二:引用的com组件:MicrosoftOfficeInteropExceldll 读取EXCEL文件
首先是Exceldll的获取,将Office安装目录下的Excelexe文件Copy到DotNet的bin目录下,cmd到该目录下,运行 TlbImp EXCELEXE Exceldll 得到Dll文件。 再在项目中添加引用该dll文件
//读取EXCEL的方法 (用范围区域读取数据)
private void OpenExcel(string strFileName)
{
object missing = SystemReflectionMissingValue;
Application excel = new Application();//lauch excel application
if (excel == null)
{
ResponseWrite("<script>alert('Can't access excel')</script>");
}
else
{
excelVisible = false; excelUserControl = true;
// 以只读的形式打开EXCEL文件
Workbook wb = excelApplicationWorkbooksOpen(strFileName, missing, true, missing, missing, missing,
missing, missing, missing, true, missing, missing, missing, missing, missing);
//取得第一个工作薄
Worksheet ws = (Worksheet)wbWorksheetsget_Item(1);
//取得总记录行数 (包括标题列)
int rowsint = wsUsedRangeCellsRowsCount; //得到行数
//int columnsint = mySheetUsedRangeCellsColumnsCount;//得到列数
//取得数据范围区域 (不包括标题列)
Range rng1 = wsCellsget_Range("B2", "B" + rowsint); //item
Range rng2 = wsCellsget_Range("K2", "K" + rowsint); //Customer
object[,] arryItem= (object[,])rng1Value2; //get range's value
object[,] arryCus = (object[,])rng2Value2;
//将新值赋给一个数组
string[,] arry = new string[rowsint-1, 2];
for (int i = 1; i <= rowsint-1; i++)
{
//Item_Code列
arry[i - 1, 0] =arryItem[i, 1]ToString();
//Customer_Name列
arry[i - 1, 1] = arryCus[i, 1]ToString();
}
ResponseWrite(arry[0, 0] + " / " + arry[0, 1] + "#" + arry[rowsint - 2, 0] + " / " + arry[rowsint - 2, 1]);
}
excelQuit(); excel = null;
Process[] procs = ProcessGetProcessesByName("excel");
foreach (Process pro in procs)
{
proKill();//没有更好的方法,只有杀掉进程
}
GCCollect();
}
3方法三:将EXCEL文件转化成CSV(逗号分隔)的文件,用文件流读取(等价就是读取一个txt文本文件)。
先引用命名空间:using SystemText;和using SystemIO;
FileStream fs = new FileStream("d:\\Customercsv", FileModeOpen, FileAccessRead, FileShareNone);StreamReader sr = new StreamReader(fs, SystemTextEncodingGetEncoding(936));
string str = "";
string s = ConsoleReadLine();
while (str != null)
{ str = srReadLine();
string[] xu = new String[2];
xu = strSplit(',');
string ser = xu[0];
string dse = xu[1]; if (ser == s)
{ ConsoleWriteLine(dse);break;
}
} srClose();
另外也可以将数据库数据导入到一个txt文件,实例如下:
//txt文件名
string fn = DateTimeNowToString("yyyyMMddHHmmss") + "-" + "PO014" + "txt";
OleDbConnection con = new OleDbConnection(conStr);
conOpen();
string sql = "select ITEM,REQD_DATE,QTY,PUR_FLG,PO_NUM from TSD_PO014";
//OleDbCommand mycom = new OleDbCommand("select from TSD_PO014", mycon);
//OleDbDataReader myreader = mycomExecuteReader(); //也可以用Reader读取数据
DataSet ds = new DataSet();
OleDbDataAdapter oda = new OleDbDataAdapter(sql, con);
odaFill(ds, "PO014");
DataTable dt = dsTables[0];
FileStream fs = new FileStream(ServerMapPath("download/" + fn), FileModeCreate, FileAccessReadWrite);
StreamWriter strmWriter = new StreamWriter(fs); //存入到文本文件中
//把标题写入txt文件中
//for (int i = 0; i <dtColumnsCount;i++)
//{
// strmWriterWrite(dtColumns[i]ColumnName + " ");
//}
foreach (DataRow dr in dtRows)
{
string str0, str1, str2, str3;
string str = "|"; //数据用"|"分隔开
str0 = dr[0]ToString();
str1 = dr[1]ToString();
str2 = dr[2]ToString();
str3 = dr[3]ToString();
str4 = dr[4]ToString()Trim();
strmWriterWrite(str0);
strmWriterWrite(str);
strmWriterWrite(str1);
strmWriterWrite(str);
strmWriterWrite(str2);
strmWriterWrite(str);
strmWriterWrite(str3);
strmWriterWriteLine(); //换行
}
strmWriterFlush();
strmWriterClose();
if (conState == ConnectionStateOpen)
{
conClose();
}
以上就是关于如何用c语言提取excel中的数据全部的内容,包括:如何用c语言提取excel中的数据、、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)