dataGridView加载一个excel表,excel表里面有二维表头,怎样在加载完后,dataGridView列头显示的也是二维的样子。
看了些资料,理出的思路是:1.判断是不是二维表头。2.如果是重绘列头(但是不知道代码怎么写, Orz..)。 不知道对不对,有代码的请教教我吧。
看了些资料,理出的思路是:1.判断是不是二维表头。2.如果是重绘列头(但是不知道代码怎么写, Orz..)。 不知道对不对,有代码的请教教我吧。
public XmlDocument ReadExcelConfig(string excelFileName)
{
Excel.Application xlApp = new Excel.ApplicationClass(); if (xlApp == null)
{
System.Windows.Forms.MessageBox.Show("EXCEL could not be started. Check that your office installation and project references are correct.");
return null;
}
Excel.Workbook xlWorkBook = xlApp.Workbooks.Open(excelFileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing); Excel.Worksheet xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); Excel.Range excelRange = xlWorkSheet.UsedRange;
int RowCount = excelRange.Rows.Count; int ColCount = excelRange.Columns.Count; ReportDataGridConfig config = new ReportDataGridConfig(RowCount, ColCount); /*--------------------------------------读取数据-----------------------------------------------*/
for (Int32 i = 1; i <= RowCount; i++)
{
for (Int32 j = 1; j <= ColCount; j++)
{
Excel.Range cellRange = (Excel.Range)excelRange.Cells[i, j]; XmlElement XmlCellElement = config.GetCellConfig(i - 1, j - 1); // 单元格 if ((bool)cellRange.MergeCells) // 合并单元格
{
Int32 RowSpan = cellRange.MergeArea.Rows.Count;
Int32 ColSpan = cellRange.MergeArea.Columns.Count;
Int32 RowOffset = i - cellRange.MergeArea.Row;
Int32 ColOffset = j - cellRange.MergeArea.Column; Int32 MergeMode = 0x0001; if (RowOffset == 0 && ColOffset == 0)
MergeMode = MergeMode | 0x1000; if (ColOffset == ColSpan - 1)
MergeMode = MergeMode | 0x0100; if (RowOffset == RowSpan - 1)
MergeMode = MergeMode | 0x0010; XmlCellElement.SetAttribute("MergeMode", MergeMode.ToString()); XmlCellElement.SetAttribute("RowSpan", Convert.ToString(RowSpan));
XmlCellElement.SetAttribute("ColSpan", Convert.ToString(ColSpan)); XmlCellElement.SetAttribute("RowOffset", Convert.ToString(RowOffset));
XmlCellElement.SetAttribute("ColOffset", Convert.ToString(ColOffset)); Excel.Range cellStartRange = (Excel.Range)excelRange.Cells[cellRange.MergeArea.Row, cellRange.MergeArea.Column]; if (cellStartRange.Value2 == null)
XmlCellElement.InnerText = "";
else
XmlCellElement.InnerText = cellStartRange.Value2.ToString();
}
else
{
XmlCellElement.SetAttribute("MergeMode", "0"); if (cellRange.Value2 == null)
XmlCellElement.InnerText = "";
else
XmlCellElement.InnerText = cellRange.Value2.ToString();
}
}
}
////设置禁止弹出保存和覆盖的询问提示框
// xlApp.DisplayAlerts = false;
// xlApp.AlertBeforeOverwriting = false; xlWorkBook.Close(true, null, null);
xlApp.Quit();
releaseObject(xlWorkSheet);
releaseObject(xlWorkBook);
releaseObject(xlApp);
return config.XmlConfigDoc;
} private void releaseObject(object obj)
{
try
{
System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
obj = null;
}
catch
{
obj = null;
}
finally
{
GC.Collect();
GC.Collect();
}
}
/// <summary>
/// 重写方法
/// </summary>
/// <param name="e"></param>
protected override void OnCellPainting(DataGridViewCellPaintingEventArgs e)
{
try
{
if (e.RowIndex >= 0 && e.ColumnIndex >= 0)
{
DataGridCellConfig cellConfig = this.GetDataGridCellConfig(e.RowIndex, e.ColumnIndex); int MergeMode = cellConfig.MergeMode;
if (MergeMode >= 1)
{
SolidBrush gridBrush = new SolidBrush(this.GridColor);
SolidBrush backBrush = new SolidBrush(e.CellStyle.BackColor); SolidBrush fontBrush = new SolidBrush(e.CellStyle.ForeColor); Pen gridLinePen = new Pen(gridBrush); /*---------------------------1 擦掉合并单元格-------------------------------------*/
// e.Graphics.FillRectangle(backBrush, e.CellBounds); if (this.SelectedCells.Contains(this.Rows[e.RowIndex].Cells[e.ColumnIndex]))
e.Graphics.FillRectangle(new SolidBrush(this.DefaultCellStyle.SelectionBackColor), e.CellBounds);
else
e.Graphics.FillRectangle(backBrush, e.CellBounds); /*---------------------------2 绘制合并单元格右边线和下边线-------------------------------------*/
if ((MergeMode & 0x0100) == 0x0100) // 右边线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom); if ((MergeMode & 0x0010) == 0x0010) // 下边线
e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1); /*---------------------------2 绘制合并单元格文字-------------------------------------*/
this.PaintingFont(e); // 绘制文字,省去 e.Handled = true;
} base.OnCellPainting(e);
}
}
catch { }
}
/// MergeMode: 0xmmmm(十六进制),一个单元格单元格既可以是左上角和右边,例如0x1101,也可以是左上角和下边,例如0x1011
/// 未合并状态 : 0x0000(四位都为0)
/// 合并状态,该单元格是左上角【LeftAndTop】 : 0x1mmm(第一位数字是1)
/// 合并状态,该单元格是右边【Right】 : 0xm1mm(第二位数字是1)
/// 合并状态,该单元格是下边【Bottom】 : 0xmm1m(第三位数字是1)
/// 合并状态 : 0xmmm1(第四位数字是1)