Excel ドキュメントを読み取り、セルに含まれるデータをデータベースに配置する必要があります。ただし、私が気付いた問題は、行からデータを読み取るようになったときに、フォームに表示される順序で出てこないことです。どうすればこれを解決できますか?
public void getrowdata(){
IEnumerable<Row> dataRows = from row in s.worksheetpart.Worksheet.Descendants<Row>()
where row.RowIndex > 6
select row;
// extract the data in the row in order
foreach (Row row in dataRows)
{
var cellValues = from cell in row.Descendants<Cell>()
select ((cell.CellValue != null && cell.DataType != null && cell.DataType.HasValue)
&& (sharedString.HasChildren && int.Parse(cell.CellValue.InnerText) < sharedString.ChildElements.Count)
? sharedString.ChildElements[int.Parse(cell.CellValue.InnerText)].InnerText
: ((cell.CellValue != null && cell.CellValue.InnerText != null) ? cell.CellValue.InnerText : String.Empty));
//--cellValues.toArray() and then access each cell via index in array
}
public void ReadDSheetsToBuffer()
{
try
{
//Open the Excel workbook.
using (SpreadsheetDocument document = SpreadsheetDocument.Open(file.FullName, true))
{
//References to the workbook and Shared String Table.
workBook = document.WorkbookPart.Workbook;
workSheets = workBook.Descendants<Sheet>();
sharedStrings = document.WorkbookPart.SharedStringTablePart.SharedStringTable;
ExtractSheetstoMemory2(document);
}
}
catch (Exception ex)
{
throw ex.GetBaseException();
}
}
Sample File で見つけたSample Excel File I read with code
以下は、行のセルに格納された値にアクセスする方法のタイプです。. .
if (values[228] != null)
itemdetail.Custom1 = rowvalues[228].Trim();
if (values[229] != null)
itemdetail.Custom2 = rowvalues[229].Trim();
if (values[230] != null)
itemdetail.Custom3 = rowvalues[230].Trim();
if (values[231] != null)
itemdetail.Custom4 = rowvalues[231].Trim();
if (values[232] != null)
itemdetail.Custom5 = rowvalues[232].Trim();
if (values[233] != null)
itemdetail.Custom6 = rowvalues[233].Trim();
セル参照を使用してセル内部テキストにアクセスしようとする試み
foreach (Row row in dataRows)
{
if (row.RowIndex > 6)
{
String theCell = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(1) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell2 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(2) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell3 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(3) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell4 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(4) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell5 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(5) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell6 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(6) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell7 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(7) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell8 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(8) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell9 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(9) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell10 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(10) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell11 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(11) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell112 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(12) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell13 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(13) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell14 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(14) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
String theCell15 = row.Descendants<Cell>().Where(c => c.CellReference == ExcelColumnFromNumber(15) + row.RowIndex.ToString()).FirstOrDefault().InnerText;
}
}