C# DevExpress spreadsheetControl的基本使用方法 Excel插件

 //打开Excel
        private void barButtonItem1_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (Convert.ToInt32(trv_menu.SelectedNode.Tag) == 0)
            {
                MessageBox.Show("请先选择业务", "提示信息", MessageBoxButtons.OK);
                return;
            }
            openFileDialog1.Filter = "*.xls|*.xlsx";
            openFileDialog1.RestoreDirectory = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            { 
                spreadsheetControl1.LoadDocument(openFileDialog1.FileName);
            }
            spreadsheetControl1.ReadOnly = false;
            //barButtonItem_Save.Visibility =DevExpress.XtraBars. BarItemVisibility.Always;
            barButtonItem_Save.Enabled = true;
        }
//读取Excele方法
  //当前数据行数
            int rowCount = worksheet.Cells.CurrentRegion.RowCount;
            //当前数据列数
            int columnCount = worksheet.Cells.CurrentRegion.ColumnCount;
            if (rowCount == 1 && columnCount == 1)
            { 
                MessageBox.Show("内容不能为空", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            for (int i = 0; i < rowCount; i++)
            { 
                for (int j = 0; j < columnCount; j++)
                {
                    var cellValue = spreadsheetControl1.ActiveWorksheet.Cells[i, j].Value;
                 }
            }
//从数据库里读取到Excel
        SetCellText(spreadsheetControl1.ActiveWorksheet, location, item.F_Name, true);
 /// 
        /// 从数据库里读取到Excel
        /// 
        /// 
        /// 位置格式如A1 B2
        /// 值
        /// 是否加粗
        private void SetCellText(Worksheet workSheet, string coordinates, string coordValue, bool isBold)
        {
            workSheet.Cells[coordinates].Value = coordValue;
            workSheet.Cells[coordinates].Style.Font.Bold = isBold;
        }


 

你可能感兴趣的:(DevExpress)