永发信息网

C#读excel文件中的一个小问题

答案:2  悬赏:20  手机版
解决时间 2021-01-26 20:35
  • 提问者网友:几叶到寒
  • 2021-01-26 06:25
tablenames[i] = table.Rows[i][2].ToString()是什么意思,后面的注释说是获取Excel的表名,可是这不是去表中每行的第二列么,求高手解答,如果是获取表名,有没有不是没影响么,为什么把这行注掉就不能运行了呢
最佳答案
  • 五星知识达人网友:雾月
  • 2021-01-26 08:01
你说的没错,那确实是第二列,但那个第二列取的就是表名(也就是下面那个TABLE_NAME),它所取的数据是excel的结构表,并不是你的excel中单元格中实际的数据,如果后面程序中用到了tablenames,那当然不能注释,否则程序就会出错。

你估计看到的代码是形如这样的cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
new Object[] {null, null, null, "TABLE"});

这个OleDbSchemaGuid 类型和限制规则的对象都对应于 GetOleDbSchemaTable 方法返回的 DataTable 中的一行。每个限制列对应于 DataTable 的一列,后面是基于 OleDbSchemaGuid 字段的其他架构信息。

例如,当您使用以下代码时,返回的 DataTable的每一行是一个数据库表:
schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables,
new Object[] {null, null, null, "TABLE"});

DataTable
中返回的每一列是限制列(TABLE_CATALOG、TABLE_SCHEMA、TABLE_NAME、TABLE_TYPE),后面是
TABLE_GUID、DESCRIPTION、TABLE_PROPID、DATE_CREATED 和 DATE_MODIFIED 的其他架构列。

若要获得列名称的列表(即字段描述符,如 TABLE_CATALOG、TABLE_SCHEMA 和
TABLE_NAME),您可以使用列的位置顺序。注意 Columns 数组的元素下标是从 0 开始的:
for (int i = 0; i < schemaTable.Columns.Count; i++) {
Console.WriteLine(schemaTable.Columns[i].ToString());
}
全部回答
  • 1楼网友:渊鱼
  • 2021-01-26 09:22

给你一个类,直接调用相关方法就可以了,要引用aspose.cells

using system; using system.collections.generic; using system.text; using aspose.cells; using system.data;

namespace sales {     public class asposeexcel     {     private string outfilename = "";     private string fullfilename = "";     private workbook book = null;     private worksheet sheet = null;  

    public asposeexcel(string outfilename, string tempfilename)//导出     {     outfilename = outfilename;     book = new workbook();     book.open("model.xls");     sheet = book.worksheets[0];     }

    public asposeexcel(string fullfilename)//导入     {     fullfilename = fullfilename;     //book = new workbook();     //book.open(tempfilename);     //sheet = book.worksheets[0];     }

    private void addtitle(string title, int columncount)     {     sheet.cells.merge(0, 0, 1, columncount);     sheet.cells.merge(1, 0, 1, columncount);

    cell cell1 = sheet.cells[0, 0];     cell1.putvalue(title);     cell1.style.horizontalalignment = textalignmenttype.center;     cell1.style.font.name = "黑体";     cell1.style.font.size = 14;     cell1.style.font.isbold = true;

    cell cell2 = sheet.cells[1, 0];     cell1.putvalue("查询时间:" + datetime.now.tolocaltime());     cell2.setstyle(cell1.style);     }

    private void addheader(datatable dt)     {     cell cell = null;     for (int col = 0; col < dt.columns.count; col++)     {     cell = sheet.cells[0, col];     cell.putvalue(dt.columns[col].columnname);     cell.style.font.isbold = true;     }     }

    private void addbody(datatable dt)     {     for (int r = 0; r < dt.rows.count; r++)     {     for (int c = 0; c < dt.columns.count; c++)     {     sheet.cells[r + 1, c].putvalue(dt.rows[r][c].tostring());     }     }     }

    public boolean datatabletoexcel(datatable dt)     {     //datatable dt_header;     //datarow dr = dt_header.rows[0];     //foreach (datacolumn c in dt.columns)     //{     //    dr.c.columnname);     //}     //dt_header.rows.add();     //   string dfd= dt.getsheet(strsheetname).getparameter(1).name;     boolean yn = false;     try     {     //sheet.name = sheetname;

    //addtitle(title, dt.columns.count);     //addheader(dt_header);     addbody(dt);

    sheet.autofitcolumns();     //sheet.autofitrows();

    book.save(outfilename);     yn = true;     return yn;     }     catch (exception e)     {     return yn;     // throw e;     }     }

    public datatable exceltodatatalbe()//导入     {     workbook book = new workbook();     book.open(fullfilename);     worksheet sheet = book.worksheets[0];     cells cells = sheet.cells;     //获取excel中的数据保存到一个datatable中     datatable dt_import = cells.exportdatatableasstring(0, 0, cells.maxdatarow + 1, cells.maxdatacolumn + 1, false);     // dt_import.     return dt_import;     }

    } }

我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯