admin管理员组文章数量:1531475
2023年12月18日发(作者:)
//添加引用
using System;
using System.Collections.Generic;
using Systnq;
using Syste;
using System.Web.UI;
using System.Web.bControls;
using Microsofice.Interoel;
using Systegnostics;
//网页加载
protected void Page_Load(object sender, EventArgs e)
{
string path = ServpPath("cachefile/33.xlsx");
OpenExcel(path);
}
//读取EXCEL的方法 (用范围区域读取数据)
///
/// 利用com组件来读取EXCEL生成table的方法
///
///
private void OpenExcel(string strFileName)
{
try
{
object missing = System.Reflection.Missing.Value;
Application excel = new Application();//lauch excel application
if (excel == null)
{
Response.Write("alert('Can't access excel')script>");
}
else
{
exceible = false;
excerControl = true;
// 以只读的形式打开EXCEL文件
Workbook wb = excel.Applicatiokbookn(strFileName, missing,
true, missing, missing, missing,missing, missing, missing, true, missing, missing,
missing, missing, missing);
//取得第一个工作薄
Worksheet ws = (Worksheet)wb.Worksheets.get_Item(1);
//取得总记录行数 (包括标题列)
int rowsint = ws.UsedRange.Cells.Rows.Count; //得到行数
int columnsint = ws.UsedRange.Cellumns.Count;//得到列数
//取得数据范围区域 (包括标题列)
//Range rng1 = ws.Cells.get_Range("A1", "A" + rowsint); //item
Responste("<table width='100%' border='1' align='center' cellpadding='0'
cellspacing='0'>");
for (int i = 0; i <= rowsint; i++)//行
{
Response.Write("
for (int j = 0; j <= columnsint; j++)//列
{
string value = string.Empty;//单元格的值
string colname = string.Empty;//列名
Response.Write("
if (i == 0&&j==0)
{
value=" ";//第一行第一列
}else if(i==0&&j>0){
value = this.GetColumnNameByIndex(j);//添加行头
}else{
if (j==0)
{
value = (i).ToString();
}
else
{
colname = this.GetColumnNameByIndex(j) + i;
if (ws.get_Range(colname).Value2 == null)
{
value = " ";
}
else
{
value = t_Range(colname).Value2.ToString();
}
}
}
Response.Write(value);
Response.Write("td>");
}
Response.Write("
}
Response.Write("");
}
excit(); excel = null;
Process[] procs = Process.GetProcessesByName("excel");
foreach (Process pro in procs)
{
pll();//没有更好的方法,只有杀掉进程
}
llect();
}
catch (Exception ex)
{
Response.Write("显示信息失败:" + ex);
}
}
///
/// 根据列的数字索引返回字母索引
///
///
/// <returns>returns>
private string GetColumnNameByIndex(int columnIndex) {
string result = string.Empty;
if (columnIndex>-1)
{
string[] columnName = { "", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K",
"L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" };
result = columnIndex > 26 ? columnName[columnIndex / 26].ToString() +
columnName[columnIndex % 26].ToString() : columnName[columnIndex].ToString();
}
return result;
}
版权声明:本文标题:利用Com组件来读取EXCEL生成table表格的显示方法 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dongtai/1702851927a27341.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论