码迷,mamicode.com
首页 > 其他好文 > 详细

导入excel精华版

时间:2015-07-24 06:53:19      阅读:132      评论:0      收藏:0      [点我收藏+]

标签:

//须引入 NPOI, NPOI.OOXML, NPOI.Openxml4Net, NPOI.OpenxmlFormats等程序集 自己去下载吧

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Holworth.Utility;
using Microsoft.SqlServer.Server;

namespace ConsoleApplication25
{
public enum DataTypeDimesion
{
浮点型 = 0,
整型 = 1,
日期 = 2,
字符串 = 3
}

public class ExcelDataMap
{
public int RowIndex { get; set; }
public string ColumnName { get; set; }
public DataTypeDimesion DataType { get; set; }
}

class Program
{
public static List<ExcelDataMap> ExcelDataMaps = new List<ExcelDataMap>();

public static string ColNameMapValue(DataTable table, int rowIndex,int colIndex)
{
return table.Rows[rowIndex][colIndex].ToString();
}

static void Main(string[] args)
{
//EXCEL 列映射 start
ExcelDataMaps.Add(new ExcelDataMap() { RowIndex = 0, ColumnName = "CLIENT_NAME", DataType = DataTypeDimesion.字符串 });
ExcelDataMaps.Add(new ExcelDataMap() { RowIndex = 1, ColumnName = "COMPANY_NAME", DataType = DataTypeDimesion.字符串 });

ExcelDataMaps.Add(new ExcelDataMap() { RowIndex = 3, ColumnName = "TOTAL_ASSET", DataType = DataTypeDimesion.浮点型 });
ExcelDataMaps.Add(new ExcelDataMap() { RowIndex = 4, ColumnName = "SALE_REVENUE", DataType = DataTypeDimesion.浮点型 });

ExcelDataMaps.Add(new ExcelDataMap() { RowIndex = 6, ColumnName = "RETURN_ON_EQUITY", DataType = DataTypeDimesion.浮点型 });
ExcelDataMaps.Add(new ExcelDataMap() { RowIndex = 7, ColumnName = "SALE_PROFIT", DataType = DataTypeDimesion.浮点型 });
ExcelDataMaps.Add(new ExcelDataMap() { RowIndex = 8, ColumnName = "RETURN_TOTAL_ASSET", DataType = DataTypeDimesion.浮点型 });
ExcelDataMaps.Add(new ExcelDataMap() { RowIndex = 9, ColumnName = "INVENTORY_TURNOVER", DataType = DataTypeDimesion.浮点型 });
ExcelDataMaps.Add(new ExcelDataMap() { RowIndex = 10, ColumnName = "FIXED_ASSET_TURNOVER", DataType = DataTypeDimesion.浮点型 });
ExcelDataMaps.Add(new ExcelDataMap() { RowIndex = 11, ColumnName = "TOTAL_ASSET_TUNOVER", DataType = DataTypeDimesion.浮点型 });

ExcelDataMaps.Add(new ExcelDataMap() { RowIndex = 13, ColumnName = "ACCOUNT_RECEIVABLE_TURNOVER", DataType = DataTypeDimesion.浮点型 });
ExcelDataMaps.Add(new ExcelDataMap() { RowIndex = 14, ColumnName = "CURRENT_RATIO", DataType = DataTypeDimesion.浮点型 });
ExcelDataMaps.Add(new ExcelDataMap() { RowIndex = 15, ColumnName = "QUICK_RATIO", DataType = DataTypeDimesion.浮点型 });
ExcelDataMaps.Add(new ExcelDataMap() { RowIndex = 16, ColumnName = "ASSET_LIABILITY_RATIO", DataType = DataTypeDimesion.浮点型 });
//EXCEL 列映射 end


string fileName = @"D:\data of office.xls";
DataSet ds = new DataSet();
DataTable dt = new DataTable();
ds = ExcelHelper.ReadExcelAllSheets(fileName, 0, false);
dt = ds.Tables[0];
int colStartIndex= 5;//每列为一条记录 这里从第5列开始
//从0行开始读取Excel
int rowIndex = 0;
string columns = "";
string sql = "";
//遍历每一行 这里相当于每个字段
while (true)
{
//最后一列遇到结束的时候跳出循环不再拼接sql语句
//if (string.IsNullOrEmpty(dt.Rows[0][colStartIndex].ToString()))
try
{
if (dt.Rows[0][colStartIndex].ToString() == "xxend")
{

break;

}
}
catch (Exception)
{

return;
}
sql += "\r\n insert into ( ";
ExcelDataMaps.ForEach(e => { columns += e.ColumnName + ","; });
columns = columns.TrimEnd(‘,‘);
//sql 先拼接头部
sql += columns;
sql += ")";
sql += "\r\n";
sql += "values(";

foreach (ExcelDataMap dataMap in ExcelDataMaps)
{
string valueObject = ColNameMapValue(dt, dataMap.RowIndex, colStartIndex);

switch (dataMap.DataType)
{
case DataTypeDimesion.字符串:
sql += "‘" + valueObject + "‘"+",";
break;
case DataTypeDimesion.整型:
int i = 0;
if (string.IsNullOrEmpty(valueObject))
{
valueObject = "0";
}
int.TryParse(valueObject, out i);
sql += i + ",";
break;
case DataTypeDimesion.浮点型:
decimal f = 0;
if (string.IsNullOrEmpty(valueObject))
{
valueObject = "0";
}
Decimal.TryParse(valueObject, out f);
sql += f + ",";
break;
break;
case DataTypeDimesion.日期:
if (!string.IsNullOrEmpty(valueObject))
{
valueObject = (string.Format("to_date(‘{0}‘,‘yyyy-mm-dd hh24:mi:ss‘)", valueObject));
}
else
{
valueObject = "‘‘";

}
sql += valueObject + ",";
break;

}

}
sql = sql.TrimEnd(‘,‘);
sql += ")";
colStartIndex++;
columns = "";
}


}
}
}

导入excel精华版

标签:

原文地址:http://www.cnblogs.com/kexb/p/4672280.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!