C#读excel文件中的一个小问题
- 提问者网友:几叶到寒
- 2021-01-26 06:25
- 五星知识达人网友:雾月
- 2021-01-26 08:01
你估计看到的代码是形如这样的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; }
} }