使用C#和Excel进行报表开发(八)-用程序绑定数据源

本文演示一个简单的办法,并使用程序将一个dataset中的内容填充到指定的格子中,目的是尽可能的通用,从而避免C#代码必须知道Excel文件中字段和内容的位置的情况。

先制作一个简单的Excel文件作为模板,为了防止要填充的Cell中的内容和标题的内容一样,所以要填充内容的Cell中的内容是“$” + 字段名(要和DataTable中的列名一致),效果如图:
使用C#和Excel进行报表开发(八)-用程序绑定数据源
创建一个Winform程序,给窗体上添加两个按钮,代码分别为:
创建Xml:

private void button1_Click( object sender,EventArgse)
{
DataColumndcName
=newDataColumn("name",typeof(string));
DataColumndcAge
=newDataColumn("age",typeof(int));
DataColumndcMemo
=newDataColumn("memo",typeof(string));

DataTabledt
=newDataTable();
dt.Columns.Add(dcName);
dt.Columns.Add(dcAge);
dt.Columns.Add(dcMemo);

DataRowdr
=dt.NewRow();
dr[
"name"]="dahuzizyd";
dr[
"age"]="20";
dr[
"memo"]="dahuzizyd.cnblogs.com";

dt.Rows.Add(dr);
dt.AcceptChanges();

DataSetds
=newDataSet();
ds.Tables.Add(dt);

ds.WriteXml(Application.StartupPath
+"\\ExcelBindingXml.xml");


}



提取xml并且加载到Excel模板上,再另存:

private void button2_Click( object sender,EventArgse)
{
DataSetds
=newDataSet();
ds.ReadXml(Application.StartupPath
+"\\ExcelBindingXml.xml");

Excel.Applicationm_objExcel
=null;

Excel._Workbookm_objBook
=null;

Excel.Sheetsm_objSheets
=null;

Excel._Worksheetm_objSheet
=null;

Excel.Rangem_objRange
=null;

objectm_objOpt=System.Reflection.Missing.Value;

try
{

m_objExcel
=newExcel.Application();
m_objBook
=m_objExcel.Workbooks.Open(Application.StartupPath+"\\ExcelTemplate.xls",m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt);
m_objSheets
=(Excel.Sheets)m_objBook.Worksheets;
m_objSheet
=(Excel._Worksheet)(m_objSheets.get_Item(1));

foreach(DataRowdrinds.Tables[0].Rows)
{
for(intcol=0;col<ds.Tables[0].Columns.Count;col++)
{
for(intexcelcol=1;excelcol<8;excelcol++)
{
for(intexcelrow=1;excelrow<5;excelrow++)
{
stringexcelColName=ExcelColNumberToColText(excelcol);

m_objRange
=m_objSheet.get_Range(excelColName+excelrow.ToString(),m_objOpt);

if(m_objRange.Text.ToString().Replace("$","")==ds.Tables[0].Columns[col].ColumnName)
{
m_objRange.Value2
=dr[col].ToString();
}

}


}

}

}


m_objExcel.DisplayAlerts
=false;
m_objBook.SaveAs(Application.StartupPath
+"\\ExcelBindingXml.xls",m_objOpt,m_objOpt,
m_objOpt,m_objOpt,m_objOpt,Excel.XlSaveAsAccessMode.xlNoChange,

m_objOpt,m_objOpt,m_objOpt,m_objOpt,m_objOpt);

}

catch(Exceptionex)
{
MessageBox.Show(ex.Message);
}

finally
{
m_objBook.Close(m_objOpt,m_objOpt,m_objOpt);
m_objExcel.Workbooks.Close();
m_objExcel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objBook);
System.Runtime.InteropServices.Marshal.ReleaseComObject(m_objExcel);
m_objBook
=null;
m_objExcel
=null;
GC.Collect();
}

}


下面是一个辅助函数,主要是将整数的列序号转换到Excel用的以字母表示的列号,Excel最大列数为255。

private string ExcelColNumberToColText( int colNumber)
{
stringcolText="";

intcolTextLength=colNumber/26;
intcolTextLast=colNumber%26;

if(colTextLast!=0)
{
switch(colTextLength)
{
case0:break;
case1:colText="A";break;
case2:colText="B";break;
case3:colText="C";break;
case4:colText="D";break;
case5:colText="E";break;
case6:colText="F";break;
case7:colText="G";break;
case8:colText="H";break;
case9:colText="I";break;
default:break;
}

}

else
{
switch(colTextLength)
{
case1:colText="";break;
case2:colText="A";break;
case3:colText="B";break;
case4:colText="C";break;
case5:colText="D";break;
case6:colText="E";break;
case7:colText="F";break;
case8:colText="G";break;
case9:colText="H";break;
default:break;
}

}


switch(colTextLast)
{
case0:colText=colText+"Z";break;
case1:colText=colText+"A";break;
case2:colText=colText+"B";break;
case3:colText=colText+"C";break;
case4:colText=colText+"D";break;
case5:colText=colText+"E";break;
case6:colText=colText+"F";break;
case7:colText=colText+"G";break;
case8:colText=colText+"H";break;
case9:colText=colText+"I";break;
case10:colText=colText+"J";break;
case11:colText=colText+"K";break;
case12:colText=colText+"L";break;
case13:colText=colText+"M";break;
case14:colText=colText+"N";break;
case15:colText=colText+"O";break;
case16:colText=colText+"P";break;
case17:colText=colText+"Q";break;
case18:colText=colText+"R";break;
case19:colText=colText+"S";break;
case20:colText=colText+"T";break;
case21:colText=colText+"U";break;
case22:colText=colText+"V";break;
case23:colText=colText+"W";break;
case24:colText=colText+"X";break;
case25:colText=colText+"Y";break;

default:break;
}


returncolText;
}

运行完成后,生成的Excel如下图:
使用C#和Excel进行报表开发(八)-用程序绑定数据源

你可能感兴趣的:(C++,c,Excel,C#,WinForm)