乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > <em>asp.net</em>(C#)如何将<em>excel</em>

<em>asp.net</em>(C#)如何将<em>excel</em>

作者:乔山办公网日期:

返回目录:excel表格制作


是用C#的吧!调用底下的方法,传递Excel文件所在的路径进去就可以了,它返回一张表,表里面包含sheet的名称/// <summary/// 获取EXCEL的表 表名字列/// </summarypublic DataTable GetExcelTableName(string p_ExcelFile){try{if (System.IO.File.Exists(p_ExcelFile)){OleDbConnection _ExcelConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=\"Excel 8.0\";Data Source=" + p_ExcelFile);

myConn = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;data source=" + openFileDialog1.FileName + ";Extended Properties=Excel 8.0;Persist Security Info=False");
DataTable dt = myConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
foreach (DataRow r in dt.Rows)
esComboBoxEdit9.Properties.Items.Add(r["TABLE_NAME"].ToString());
Com组件的方式读取Excel :
这种方式需要先引用 Microsoft.Office.Interop.Excel 。首选说下这种方式的优缺点
优点:可以非常灵活的读取Excel中的数据
缺点:如果是Web站点部署在IIS上时,还需要服务器机子已安装了Excel,有时候还需要为配置IIS权限。最重要的一点因为是基于单元格方式读取的,所以数据很慢。
代码如下:
DataTable GetDataFromExcelByCom(bool hasTitle = false)
{
OpenFileDialog openFile = new OpenFileDialog();
openFile.Filter = "Excel(*.xlsx)|*.xlsx|Excel(*.xls)|*.xls";
openFile.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
openFile.Multiselect = false;
if (openFile.ShowDialog() == DialogResult.Cancel) return null;
var excelFilePath = openFile.FileName;

Excel.Application app = new Excel.Application();
Excel.Sheets sheets;
object oMissiong = System.Reflection.Missing.Value;
Excel.Workbook workbook = null;
DataTable dt = new DataTable();

try
{
if (app == null) return null;
workbook = app.Workbooks.Open(excelFilePath, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong,
oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong, oMissiong);
sheets = workbook.Worksheets;

//将数据读入到DataTable中
Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);//读取第一张表
if (worksheet == null) return null;

int iRowCount = worksheet.UsedRange.Rows.Count;
int iColCount = worksheet.UsedRange.Columns.Count;
//生成列头
for (int i = 0; i < iColCount; i++)
{
var name = "column" + i;
if (hasTitle)
{
var txt = ((Excel.Range)worksheet.Cells[1, i + 1]).Text.ToString();
if (!string.IsNullOrWhiteSpace(txt)) name = txt;
}
while (dt.Columns.Contains(name)) name = name + "_1";//重复行e799bee5baa6e59b9ee7ad94365名称会报错。
dt.Columns.Add(new DataColumn(name, typeof(string)));
}
//生成行数据
Excel.Range range;
int rowIdx = hasTitle ? 2 : 1;
for (int iRow = rowIdx; iRow <= iRowCount; iRow++)
{
DataRow dr = dt.NewRow();
for (int iCol = 1; iCol <= iColCount; iCol++)
{
range = (Excel.Range)worksheet.Cells[iRow, iCol];
dr[iCol - 1] = (range.Value2 == null) ? "" : range.Text.ToString();
}
dt.Rows.Add(dr);
}
return dt;
}
catch { return null; }
finally
{
workbook.Close(false, oMissiong, oMissiong);
System.Runtime.InteropServices.Marshal.ReleaseComObject(workbook);
workbook = null;
app.Workbooks.Close();
app.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
app = null;
}
}

openFileDialog1.ShowDialog();

oledbconn1.ConnectionString = "provider=Microsoft.Jet.OLEDB.4.0;data source="+openFileDialog1.FileName+";Extended Properties=Excel 8.0;Persist Security Info=False";

oledbconn1.Open();

DataTable dt = oledbconn1.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,new objec] {null, null, null, "TABLE"});

foreach (DataRow dr in dt.Rows)
{

comboBox1.Items.Add((String)dr["TABLE_NAME"]);
}

相关阅读

  • -access excel asp.net,asp excel

  • 乔山办公网excel表格制作
  • 鉴于你已经提抄过问题,你无需重复提问题的。给你一个思路。你可以使用Access建个MDB数据库,用Asp.net在页面增加或者修改数据,然后再从Excel打开一个连百接,查询数据库里的表。这
关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网