`

Silverlight使用Com组件操作Excel的封装类(3)使用方法

阅读更多
//方法1
//打开excel
        public bool OpenXls()
        {
            var ofd = new OpenFileDialog();
            //ofd.Filter = "Excel2003文件(*.xls)|*.xls|Excel2007文件(*.xlsx)|*.xlsx|所有文件(*.*)|*.*";
            ofd.Filter = "Excel2003文件(*.xls)|*.xls|所有文件(*.*)|*.*";
            var result = ofd.ShowDialog();
            if (result ?? false)
            {
                try
                {
                    using (FileStream fs = ofd.File.OpenRead())
                    {
                        if(xlsApp == null)
                            xlsApp = new LSOOBXlsApplication();
                        xlsApp.IsOpen = false;
                        xlsWorkBook = xlsApp.OpenWorkbook(fs, ofd.File.Extension);
                        //xlsWorkSheet = xlsWorkBook.ActiveSheet;
                    }

                    return true;
                }
                catch (Exception ex)
                {
                    //RadWindow.Alert("打开Excel文件失败, \n原因:" + ex.Message);
                    LSWindow.AlertError(ex, false);
                    return false;
                }
                finally
                {
                    //if (xlsWorkBook != null)
                    //    xlsWorkBook.Close();
                    //if(xlsApp != null)
                    //    xlsApp.Close();
                    //xlsWorkBook = null;
                    //xlsApp = null;
                }
            }
            return false;
        }

 

方法2

int iColumnCount = sheet.GetUsedRange().ColumnsCount;
int iRowCount = sheet.GetUsedRange().RowsCount;
 for (int i = 1; i < iColumnCount + 1; i++)
{
  str += sheet.GetCell(1, i).Value.ToString() + ", ";
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics