程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> 關於C# >> 使用C#和Excel進行報表開發(8)

使用C#和Excel進行報表開發(8)

編輯:關於C#

本文演示一個簡單的辦法,並使用程序將一個dataset中的內容填充到指定的格子中,目的是盡可能的通用,從而避免C#代碼必須知道Excel文件中字段和內容的位置的情況。

先制作一個簡單的Excel文件作為模板,為了防止要填充的Cell中的內容和標題的內容一樣,所以要填充內容的Cell中的內容是“$” + 字段名(要和DataTable中的列名一致),效果如圖:

創建一個Winform程序,給窗體上添加兩個按鈕,代碼分別為:

創建Xml:

private void button1_Click(object sender, EventArgs e)
{
DataColumn dcName = new DataColumn("name", typeof(string));
DataColumn dcAge = new DataColumn("age", typeof(int));
DataColumn dcMemo = new DataColumn("memo", typeof(string));
DataTable dt = new DataTable();
dt.Columns.Add(dcName);
dt.Columns.Add(dcAge);
dt.Columns.Add(dcMemo);
DataRow dr = dt.NewRow();
dr["name"] = "dahuzizyd";
dr["age"] = "20";
dr["memo"] = "dahuzizyd.cnblogs.com";
dt.Rows.Add(dr);
dt.AcceptChanges();
DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.WriteXml(Application.StartupPath +"\\ExcelBindingXml.xml");
}

提取xml並且加載到Excel模板上,再另存:

private void button2_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            ds.ReadXml(Application.StartupPath + "\\ExcelBindingXml.xml");
            Excel.Application m_objExcel = null;
            Excel._Workbook m_objBook = null;
            Excel.Sheets m_objSheets = null;
            Excel._Worksheet m_objSheet = null;
            Excel.Range m_objRange = null;
            object m_objOpt = System.Reflection.Missing.Value;
            try
            {
                m_objExcel = new Excel.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 (DataRow dr in ds.Tables[0].Rows)
                {
                    for (int col = 0; col < ds.Tables[0].Columns.Count; col++)
                    {
                        for (int excelcol = 1; excelcol < 8; excelcol++)
                        {
                            for (int excelrow = 1; excelrow < 5; excelrow++)
                            {
                                string excelColName = 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 (Exception ex)
            {
                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)
{
string colText = "";
int colTextLength = colNumber / 26;
int colTextLast = colNumber % 26;
if (colTextLast != 0)
{
switch (colTextLength)
{
case 0: break;
case 1: colText = "A"; break;
case 2: colText = "B"; break;
case 3: colText = "C"; break;
case 4: colText = "D"; break;
case 5: colText = "E"; break;
case 6: colText = "F"; break;
case 7: colText = "G"; break;
case 8: colText = "H"; break;
case 9: colText = "I"; break;
default: break;
}
}
else
{
switch (colTextLength)
{
case 1: colText = ""; break;
case 2: colText = "A"; break;
case 3: colText = "B"; break;
case 4: colText = "C"; break;
case 5: colText = "D"; break;
case 6: colText = "E"; break;
case 7: colText = "F"; break;
case 8: colText = "G"; break;
case 9: colText = "H"; break;
default: break;
}
}
switch (colTextLast)
{
case 0:colText = colText + "Z"; break;
case 1: colText = colText + "A"; break;
case 2: colText = colText + "B"; break;
case 3: colText = colText + "C"; break;
case 4: colText = colText + "D"; break;
case 5: colText = colText + "E"; break;
case 6: colText = colText + "F"; break;
case 7: colText = colText + "G"; break;
case 8: colText = colText + "H"; break;
case 9: colText = colText + "I"; break;
case 10: colText = colText + "J"; break;
case 11: colText = colText + "K"; break;
case 12: colText = colText + "L"; break;
case 13: colText = colText + "M"; break;
case 14: colText = colText + "N"; break;
case 15: colText = colText + "O"; break;
case 16: colText = colText + "P"; break;
case 17: colText = colText + "Q"; break;
case 18: colText = colText + "R"; break;
case 19: colText = colText + "S"; break;
case 20: colText = colText + "T"; break;
case 21: colText = colText + "U"; break;
case 22: colText = colText + "V"; break;
case 23: colText = colText + "W"; break;
case 24: colText = colText + "X"; break;
case 25: colText = colText + "Y"; break;
default: break;
}
return colText;
}

運行完成後,生成的Excel如下圖:

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved