openxml讀取xlsx內容

2021-10-04 06:04:39 字數 4428 閱讀 4194

var doc = new exceluntil().open(filename, false);

var shtcontentdict = doc.getsheetscontent();

int count = 0;

foreach (var item in shtcontentdict)

codes.addrange(gettextoutput(item.value, item.key, count++));

akresponse.write(response, "<" + "script" + ">" + akcommon.listjoin(codes, "\r\n") + "");

doc.close();

using system;

using system.collections.generic;

using system.linq;

using system.text;

using documentformat.openxml.packaging;

using documentformat.openxml.spreadsheet;

using documentformat.openxml;

using system.io;

using system.text.regularexpressions;

/// /// 工作簿

///

public workbookpart workbookpart

/// /// sheet集合

///

public sheets sheets

#endregion

public spreadsheetdocument open(string filename, bool isedit)

public spreadsheetdocument open(stream stream, bool isedit)

public void close()}}

public static class extenopenxml

return sheetnames.toarray();

}/// /// 獲取worksheet

///

///

///

///

public static worksheet getworksheet(this spreadsheetdocument document, string sheetname = null)

public static int getcolmax(this worksheet worksheet)

private static int getrownumber(string str)

private static int lettertoint(string str)

private static int getrowcount(string dimension)

private static string getcolumnletter(string str)

}if (index > 0)

return null;

}/// /// 數字轉字母

///

///

///

private static string inttoletter(int index)

listchars = new list();

dowhile (index > 0);

return string.join(string.empty, chars.toarray());

}/// /// 獲取共享字串

///

///

///

public static ienumerablegetsharedstringtable(this spreadsheetdocument document)

/// /// 獲取sheetdata

///

///

///

///

public static sheetdata getfirstsheetdata(this spreadsheetdocument document, string sheetname = null)

/// /// 獲取sheetdata

///

///

///

public static sheetdata getfirstsheetdata(this worksheet worksheet)

/// /// 獲取workbook

///

///

///

public static workbook getworkbook(this workbookpart workbookpart)

public static void updatecelltext(this sheetdata sheetdata, string cellname, string celltext)

/// /// 修改單元格文字

///

///

///

private static void updatecelltext(this cell cell, object celltext)

private static cellvalues getcelldatatype(object celltext)

}/// /// 修改單元格內容(文字、樣式)

///

///

///

///

private static void updatecell(this cell cell, object celltext, uint cellstyleindex)

/// /// 根據單元格獲取行索引

///

///

///

private static uint getrowindex(string cellname)

/// /// 獲取行

///

///

///

///

private static row getrow(this sheetdata sheetdata, long rowindex)

public static ienumerablegetallrow(this worksheet worksheet)

public static dictionarygetsheetscontent(this spreadsheetdocument doc)

return dict;

}public static string getrowtext(this spreadsheetdocument doc, row row, int colmin, int colmax)

dictionaryrowvalues = new dictionary();

var cells = row.elements();

foreach (var cell in cells)

listlist = new list();

for (int i = colmin; i <= colmax; i++)

return string.join("\t", list);

}/// /// 獲取單元格

///

///

///

///

private static cell getcell(this row row, string cellname)

private static cell getcell(this sheetdata sheetdata, string cellname)

/// /// 區域名稱集合

///

///

///

public static definednames getdefinednames(this workbook workbook)

/// /// 獲取單元格的值

///

///

///

///

///

public static string getcellvalue(this row row, string columncode, sharedstringtablepart sharedstringtablepart)

catch (exception)

return value;

}public static string getcellvalue(this cell cell, sharedstringtablepart sharedstringtablepart)

return value;

}#endregion

}}

參考了:

OpenXml讀取word內容的例項

openxml讀取word內容注意事項 1 使用openxml讀取word內容,word字尾必須是 docx 如果word字尾是 do程式設計客棧c 需要轉成 docx 後,才可以讀取 2 需要引入相關dll windowsbase.dll documentformat.openxml.dll 3 ...

python讀取xlsx檔案

我是在win7下讀取的。python版本是 3.5 import xlrd import re import sqlite3 def read xlsx workbook xlrd.open workbook e 20160322.xlsx booksheet workbook.sheet by n...

python讀取xlsx檔案

encoding utf 8 from openpyxl import load workbook workbook load workbook u xlsx 相對路徑,找到需要開啟的檔案位置 booksheet workbook.active 獲取當前活躍的 sheet,預設是第乙個 sheet ...