namespace _2018._7._16__Excal文档解析
{
class TweItem
{
public int index { get; set; }
public string name { get; set; }
public string dat { get; set; }
public override string ToString()
{
return string.Format("index;{0},name:{1},dat{2}", index, name, dat);
}
}
class One
{
public string index { get; set; }
public List<TweItem> Twe { get; set; }
}
class Program
{
static void Main(string[] args)
{
//Excal_Parsing();
Document_Excal();
Console.ReadKey();
}
public static void Document_Excal()
{
string fileDate = "1702.xlsx";
string connecin = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + fileDate + ";" + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
OleDbConnection odvecond = new OleDbConnection(connecin);//创建一个链接,把文件格式路径传进去
odvecond.Open();//打开链接
DataTable schemaTable = odvecond.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string tableName = schemaTable.Rows[0][0].ToString().Trim();
string spr = "select*from[Sheet1$]";//创建一个查询的语句,写着从哪个表查询
OleDbDataAdapter odap = new OleDbDataAdapter(spr, odvecond);//设置适配器,里面传命令语句以及链接
DataSet dst = new DataSet();//建立一个数据库
odap.Fill(dst);//把适配器里面的东西填充到这个新建表中
odvecond.Close();
DataTableCollection dtc = dst.Tables;//建立一个表的集合,用false方法让dst返回一个表的集合
DataTable dta = new DataTable();//建立一个新表
dta = dtc[0];//把集合里面的表取出来
DataRowCollection drc = dta.Rows;//通过rows属性把行输入到一个行集合中
One o = new One();//实例化一个存储Json的实体类
o.index = "1702-03";
o.Twe = new List<TweItem>();
foreach (DataRow item in drc)
{
TweItem twe = new TweItem();
twe.index = int.Parse(item[0].ToString());
twe.name = item[1].ToString();
twe.dat = item[2].ToString();
for (int i = 0; i < item.ItemArray.Length; i++)
{
Console.Write(item[i] + " ");//用下标取出行中的数据
}
Console.WriteLine();
o.Twe.Add(twe);
}
}
static void Excal_Parsing()
{
string documeDate = "1702-03.xls";
string connecin = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + documeDate + ";" + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";//创建一个字符串来存储我们的链接 死格式
OleDbConnection odvconbe = new OleDbConnection(connecin);//创建一个链接,把文件格式路径传进去
odvconbe.Open();//打开链接
DataTable schemaTable = odvconbe.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string tableName = schemaTable.Rows[0][0].ToString().Trim();
string spr = "select*from[Sheet1$]";//创建一个查询的语句,写着从哪个表查询
OleDbDataAdapter odap = new OleDbDataAdapter(spr, odvconbe);//设置适配器,里面传命令语句以及链接
DataSet dst = new DataSet();//建立一个数据库
odap.Fill(dst);//把适配器里面的东西填充到这个新建表中
odvconbe.Close();
DataTableCollection dtc = dst.Tables;//建立一个表的集合,用false方法让dst返回一个表的集合
DataTable dta = new DataTable();//建立一个新表
dta = dtc[0];//把集合里面的表取出来
DataRowCollection drc = dta.Rows;//通过rows属性把行输入到一个行集合中
One o = new One();//实例化一个存储Json的实体类
o.index = "1702-03";
o.Twe = new List<TweItem>();
foreach (DataRow item in drc)
{
TweItem twe = new TweItem();
twe.index = int.Parse(item[0].ToString());
twe.name = item[1].ToString();
twe.dat = item[2].ToString();
for (int i = 0; i < item.ItemArray.Length; i++)
{
Console.Write(item[i] + " ");//用下标取出行中的数据
}
Console.WriteLine();
o.Twe.Add(twe);
}
//string S = JsonMapper.ToJson(o);
//File.WriteAllText("Json.txt", S, Encoding.UTF8);
//Console.Write(S);
}
}
}