using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Data;
//using System.Drawing;
//using System.Windows.Forms;
using System.Collections;
using System.Data.OleDb;
using System.IO;
namespace ReadExcel
{
class Program
{
static string D_STATIC_DATA_TS_NAME = "static_data_ts";
static String[,] m_aExcelData;
static int m_nRealRows, m_nRealColums;
static String m_sStructName;
static String[] m_sStructDataType;
static String[] m_sStructVariable;
static String[] m_sStructAnnotation;
static int[] m_sArrayInf; //0: normal 1: array begin: 2: array middle 3: array end
//output m_OutputFileName m_aExcelData m_nRealRows m_nRealRows
static public bool ReadExcel(String sExcelFile, String Sheet)
{
DataTable ExcelTable;
DataSet ds = new DataSet();
//Excel的连接
OleDbConnection objConn;
try
{
objConn = new OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0;Data Source=" + sExcelFile + ";Extended Properties='Excel 8.0;HDR=No;IMEX=1;'");
objConn.Open();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
DataTable schemaTable = objConn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
string tableName = Sheet + "$";//schemaTable.Rows[0][2].ToString().Trim();//获取 Excel 的表名,默认值是sheet1
string strSql = "select * from [" + tableName + "]";
OleDbCommand objCmd;
OleDbDataAdapter myData;
try
{
objCmd = new OleDbCommand(strSql, objConn);
myData = new OleDbDataAdapter(strSql, objConn);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return false;
}
myData.Fill(ds, tableName);//填充数据
objConn.Close();
ExcelTable = ds.Tables[tableName];
int iColums = ExcelTable.Columns.Count;//列数
int iRows = ExcelTable.Rows.Count;//行数
//定义二维数组存储 Excel 表中读取的数据
m_aExcelData = new String[iRows, iColums];
for (int i = 0; i < iRows; i++)
for (int j = 0; j < iColums; j++)
{
//将Excel表中的数据存储到数组
m_aExcelData[i, j] = ExcelTable.Rows[i][j].ToString();
if (i == 1)
{
if (m_aExcelData[i, j] == "end")
{
// m_nRealColums = j;
}
}
if (j == 0)
{
if (m_aExcelData[i, j] == "end")
{
// m_nRealRows = i;
}
}
}
//
//记录表中有用信息的行数,有用信息是指除去表的标题和表的栏目,本例中表的用用信息是从第三行开始
m_nRealRows = 0;
m_nRealColums = 0;
//确定有用的行列数
for (int k = 0; k < ExcelTable.Rows.Count; k++)
{
if (m_aExcelData[k, 0] != "")
m_nRealRows++;
}
for (int k = 0; k < ExcelTable.Columns.Count; k++)
{
if (m_aExcelData[0, k] != "")
m_nRealColums++;
}
//格式问题
if (m_aExcelData[3, 0] != "key" && m_aExcelData[3, 0] != "")
{
return false;
}
//检查需要判断的列是否有相同属性
for (int col = 0; col < m_nRealColums; col++)
excel转ts代码
最新推荐文章于 2023-09-19 15:19:53 发布
本文介绍如何使用C#将Excel表格数据转换为TypeScript接口代码,帮助开发者快速生成强类型的数据模型,提高开发效率。
摘要由CSDN通过智能技术生成