如何在C#中优化列集数据表? [英] How to excel column set datatable in C#?

查看:68
本文介绍了如何在C#中优化列集数据表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨我读了excel数据,我想抛出我读过的exceli数据。

例如我想在文件夹中有excel并将其放入数据表中,方法与excel row,column中的值。

我尝试按照以下方式执行,但我收错了。我的一个错误如下。



这一行:



应用程序app =新的Excel.Application (); //此错误是:应用程序是Microsoft.Office.Interop.Excel.Application和System.Windows.Application之间的模糊引用。



我尝试过:



使用(var conn = new OleDbDataAdapter(选择DISTINCT [文件编号] FROM [Sheet1 $],conn); 
var dt = new System.Data.DataTable();
da.Fill(dt);

Application app = new Application (); //此行错误和此错误
工作簿book = null;
工作表sheet = null;
范围range = null;

app.Visible = false;
app.ScreenUpdating = false;
app.DisplayAlerts = false;

string Path = Path.GetDirectoryName(Assembly.GetExecutingAssembly()。CodeBase);

book = app.Workbooks.Open(Path + @,MissingValue,MissingValue,MissingValue);

range = sheet.get_Range(A1,Missing.Value);
range = range.get_End(XlDirection.xlToRight);
range = range.get_End(xlDirection.xlDown);

string downAddress = range.get_Address(false,假,xlReferenceStyle.xlA1,Type.Missing,Type.Missing);
range = sheet.get_Rane(A1,downAddress);
object [,] values =(object [,])range.Value2;

for(int j = 1; j< = values.GetLenght(1); j ++)
{
Console.Write({0},j);
}

for(int j = 1; j< = values.GetLenght(0); j ++)
{
Console.Write({0} ,J ++);
}

Console.WriteLine();

解决方案

,conn);
var dt = new System.Data.DataTable();
da.Fill(dt);

Application app = new Application(); //此行错误和此错误
工作簿book = null;
工作表sheet = null;
范围range = null;

app.Visible = false;
app.ScreenUpdating = false;
app.DisplayAlerts = false;

string Path = Path.GetDirectoryName(Assembly.GetExecutingAssembly()。CodeBase);

book = app.Workbooks.Open( Path + @,MissingValue,MissingValue,MissingValue);

range = sheet.get_Range(A1,Missing.Value);
range = range.get_End(XlDirection.xlToRight) ;
range = range.get_End(xlDirection.xlDown);

string downAddress = range.get_Address(false,false,xlReferenceStyle.xlA1,Type.Missing,Type.Missing);
range = sheet.get_Rane(A1,downAddress);
object [,] values =(object [,])range.Value2;

for(int j = 1; j< = values.GetLenght(1); j ++)
{
Console.Write({0},j);
}

for(int j = 1; j< = values.GetLenght(0); j ++)
{
Console.Write({0} ,J ++);
}

Console.WriteLine();


你得到的错误说你有两个具有相同名称空间的名称空间其中包含Application类的名称,因此您需要在此处选择正确的名称。



您可以在声明自己的名称时使用带有命名空间的类的全名应用程序实例或创建别名来解决此问题。







 使用 ExcelNS = Microsoft.Office.Interop.Excel;  //  为Excel命名空间创建别名 

应用程序app = new ExcelNS.Application();









应用程序app =  new  Microsoft.Office.Interop.Excel.Application(); 


在代码的右侧部分



应用程序app =新Excel。 Application(); 





您使用Excel.Application,因此编译器知道它是Excel命名空间中的Application类。但是在左侧只有应用程序,因此它不知道它是Excel中的Application类还是System.Windows中的Application类。所以更新左侧以消除歧义。



 Excel.Application app = new Excel.Application(); 


Hi I read the excel data and I want to throw the exceli datatable I read.
For example I want to have excel in a folder and throw it into the datatable in the same way as the values in the excel row, column.
I tried to do as follows, but I am taking errors. One of my mistakes is as follows.

This line:

Application app=new Excel.Application(); //this error is:Application is an ambiguous reference between Microsoft.Office.Interop.Excel.Application and System.Windows.Application.

What I have tried:

using(var conn=new OleDbDataAdapter("Select DISTINCT [File Number] FROM [Sheet1$]" ,conn);
var dt=new System.Data.DataTable();
da.Fill(dt);

Application app=new Application();   //this line error and this error 
Workbook book=null;
Worksheet sheet=null;
Range range=null;

app.Visible=false;
app.ScreenUpdating=false;
app.DisplayAlerts=false;

string Path=Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase);

book=app.Workbooks.Open(Path + @" ", MissingValue,MissingValue,MissingValue);

range=sheet.get_Range("A1",Missing.Value);
range=range.get_End(XlDirection.xlToRight);
range=range.get_End(xlDirection.xlDown);

string downAddress=range.get_Address(false,false,xlReferenceStyle.xlA1,Type.Missing,Type.Missing);
range=sheet.get_Rane("A1",downAddress);
object[,] values=(object[,])range.Value2;

for(int j=1; j<=values.GetLenght(1); j++)
{
Console.Write("{0}",j);
}

for(int j=1; j<=values.GetLenght(0); j++)
{
Console.Write("{0}",j++);
}

Console.WriteLine();

解决方案

" ,conn); var dt=new System.Data.DataTable(); da.Fill(dt); Application app=new Application(); //this line error and this error Workbook book=null; Worksheet sheet=null; Range range=null; app.Visible=false; app.ScreenUpdating=false; app.DisplayAlerts=false; string Path=Path.GetDirectoryName(Assembly.GetExecutingAssembly().CodeBase); book=app.Workbooks.Open(Path + @" ", MissingValue,MissingValue,MissingValue); range=sheet.get_Range("A1",Missing.Value); range=range.get_End(XlDirection.xlToRight); range=range.get_End(xlDirection.xlDown); string downAddress=range.get_Address(false,false,xlReferenceStyle.xlA1,Type.Missing,Type.Missing); range=sheet.get_Rane("A1",downAddress); object[,] values=(object[,])range.Value2; for(int j=1; j<=values.GetLenght(1); j++) { Console.Write("{0}",j); } for(int j=1; j<=values.GetLenght(0); j++) { Console.Write("{0}",j++); } Console.WriteLine();


The error you are getting says you have 2 namespaces having the same name of Application class in it so you need to choose the correct one for your use here.

You can either use the full name of the class with namespace when declaring your Application instance or create an alias to resolve this issue.



using ExcelNS = Microsoft.Office.Interop.Excel; // create alias for Excel namespace

Application app=new ExcelNS.Application();



Or

Application app=new Microsoft.Office.Interop.Excel.Application();


In the right-hand part of your code

Application app=new Excel.Application();



you use Excel.Application so the compiler knows it is the Application class in the Excel namespace. However on the left you only have "Application" so it doesn't know if it is the Application class in Excel or in System.Windows. So update the left-hand side to remove the ambiguity.

Excel.Application app=new Excel.Application();


这篇关于如何在C#中优化列集数据表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆