//打开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;
}