我有一个整数列表“myList”,对应于空的excel行,我想检索所有非空的单元格的值,这要归功于LINQ 我已经完成了这个以获得所有价值:
var colonneB = selectedRange.Rows.Cast<Excel.Range>().Select(x => x.Value2.ToString().Where(p => { x.Row != ctrl.myList.ElementAt(p); })).ToList();
我试着在不使用空单元格的情况下获取所有值:
var colonneB = selectedRange.Rows.Cast<Excel.Range>().Where(y => ctrl.myList.ForEach(p =>
{
int i = ctrl.myList.ElementAt(p);
if (i != y.Row)
{
}
}) != y.Row).Select(x => x.Value2.ToString()).ToList();
那:
int nInLastRow = worksheet.Cells.Find("*", System.Reflection.Missing.Value, System.Reflection.Missing.Value, System.Reflection.Missing.Value, Excel.XlSearchOrder.xlByRows, Excel.XlSearchDirection.xlPrevious, false, System.Reflection.Missing.Value, System.Reflection.Missing.Value).Row;
Excel.Range selectedRange = (Excel.Range)worksheet.get_Range("B2:B" + nInLastRow);
var colonneB = selectedRange.Rows.Cast<Excel.Range>().Select(x => x.Value2.ToString()).ToList();
但当然它没有编译,我尝试了其他的东西,但它也不起作用,我缺乏想法...... 谢谢你的帮助。
所有代码:
JAXBContext jc = JAXBContext.newInstance(Employee.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
QName qName = new QName("", "whatever");
JAXBElement<Employee> newRootEmployee = new JAXBElement<Employee>(qName, Employee.class, employee);
OutputStream os = new FileOutputStream("C:\\employee.xml")
marshaller.marshal(newRootEmployee , os);
答案 0 :(得分:0)
好吧,我终于找到了自己的答案(我为我感到骄傲^^) 这很简单,只需要这样做:
var colonneB = selectedRange.Rows.Cast<Excel.Range>().Where(y=>y.Value2 != null).Select(x => x.Value2.ToString()).ToList();
答案 1 :(得分:0)
List<double> list = selection.Cast<Range>().Select(r => r.Value).OfType<double>().ToList();
获取所有数字,但更快获得所有值:
object o = selection.Value;
List<double> list = (o as object[,] ?? new[,] { { o } }).OfType<double>().ToList();