C# DevExpress spreadsheetControl的基本使用方法 Excel外掛

Bingo_BIG發表於2016-11-25
 //開啟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);
 /// <summary>
        /// 從資料庫裡讀取到Excel
        /// </summary>
        /// <param name="workSheet"></param>
        /// <param name="coordinates">位置格式如A1 B2</param>
        /// <param name="coordValue">值</param>
        /// <param name="isBold">是否加粗</param>
        private void SetCellText(Worksheet workSheet, string coordinates, string coordValue, bool isBold)
        {
            workSheet.Cells[coordinates].Value = coordValue;
            workSheet.Cells[coordinates].Style.Font.Bold = isBold;
        }


 

相關文章