[英]GemBox Spreadsheet
如何將工作表中的數據檢索到字符串變量中? 好像沒有辦法。
var workbook = ExcelFile.Load("Workbook.xls");
var worksheet = workbook.Worksheets[0];
如何將工作表中的數據轉換為字符串?
請幫幫我!
您想以文本形式獲取整個工作表的數據,還是僅獲取特定的單元格區域,或僅獲取特定的單元格?
然而,這里是如何通過將整個工作表的數據轉換為 TAB 格式來將其轉換為字符串的方法:
var workbook = ExcelFile.Load("Workbook.xls");
workbook.Worksheets.ActiveWorksheet = workbook.Worksheets[0];
var options = new CsvSaveOptions(CsvType.TabDelimited);
options.Encoding = new UTF8Encoding(false);
using (var stream = new MemoryStream())
{
workbook.Save(stream, options);
string worksheetAsText = options.Encoding.GetString(stream.ToArray());
// Do something with "worksheetAsText" ...
}
或者,您可以使用 StringWriter 代替寫入流,如下所示:
using (var writer = new StringWriter())
{
workbook.Save(writer, options);
string worksheetAsText = writer.ToString();
// Do something with "worksheetAsText" ...
}
更新(對於 GemBox.Document,請參閱下面的評論):
有兩種方法可以執行此操作,一種是執行與上述類似的操作並將 DocumentModel 保存為純文本格式 (TxtSaveOptions)。
另一種方法是只使用以下內容:
var document = DocumentModel.Load("Document.docx");
string documentAsText = document.Content.ToString();
您可以使用此代碼
TextBox1.Text=worksheet.Cells[6, 0].Value;
您可以使用“Microsoft.Office.Interop.Excel”實現此目的
using Excel = Microsoft.Office.Interop.Excel;
var workbookPath = "your workbookpath";
Excel.Workbook excelWorkbook = excelApp.Workbooks.Open(workbookPath, 0, false, 5, "", "", false, Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
Excel.Sheets excelSheets = excelWorkbook.Worksheets;
string currentSheet = "Sheet1"; //name of the sheet
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(currentSheet);
var cellValue = (string)(excelWorksheet.Cells[10, 2] as Excel.Range).Value; //cell is casted to string
現在變量 cellvalue 保存了單元格 [10,2] 中的刺痛。 希望這能解決問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.