码迷,mamicode.com
首页 > 其他好文 > 详细

MyXLS案例

时间:2015-01-10 13:53:30      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

using System;
using System.Data;
using org.in2bits.MyXls;
namespace Maticsoft.Common
{
    /// <summary>
    /// 操作EXCEL导出数据报表的类
    /// Copyright (C) Maticsoft
    /// </summary>
    public class DataToExcel
    {
        public DataToExcel()
        {
        }

        /// <summary>
        /// 绑定数据库生成XLS报表
        /// using org.in2bits.MyXls;
        /// </summary>
        /// <param name="ds">获取DataSet数据集</param>
        /// <param name="xlsName">报表表名</param>
        public void toExcel(DataTable table, string xlsName)
        {
            XlsDocument xls = new XlsDocument();
            xls.FileName = xlsName + ".xls";
            int rowIndex = 1;
            int colIndex = 0;
            Worksheet sheet = xls.Workbook.Worksheets.Add("Sheet1");
            Cells cells = sheet.Cells;
            foreach (DataColumn col in table.Columns)
            {
                colIndex++;
                cells.Add(1, colIndex, col.ColumnName);
            }

            foreach (DataRow row in table.Rows)
            {
                rowIndex++;
                colIndex = 0;
                foreach (DataColumn col in table.Columns)
                {
                    colIndex++;
                    Cell cell;
                    if (col.DataType == Type.GetType("System.Int32"))
                    {
                        if (Str2Int(row[col.ColumnName].ToString()) != 0)
                        {
                            cell = cells.Add(rowIndex, colIndex, Str2Int(row[col.ColumnName].ToString()));
                        }
                    }
                    else if (col.DataType == Type.GetType("System.Decimal") || col.DataType == Type.GetType("System.Double"))
                    {
                        if (Str2Double(row[col.ColumnName].ToString()) != 0)
                        {
                            cell = cells.Add(rowIndex, colIndex, Str2Double(row[col.ColumnName].ToString()));
                        }
                    }
                    else
                    {
                        if (!row[col.ColumnName].ToString().Equals("1900-01-01"))
                            cell = cells.Add(rowIndex, colIndex, row[col.ColumnName].ToString());
                    }          
                }
            }
            xls.Send();
        }

        public DataTable FromExcel(string filepath)
        {
            XlsDocument xls = new XlsDocument(filepath);
            Worksheet sheet = xls.Workbook.Worksheets["Sheet1"];
            DataTable dt = new DataTable();
            DataColumn cl = new DataColumn();
            cl = new DataColumn("ID", System.Type.GetType("System.String"));
            dt.Columns.Add(cl);
            cl = new DataColumn("EID", System.Type.GetType("System.String"));
            dt.Columns.Add(cl);
            cl = new DataColumn("DID", System.Type.GetType("System.String"));
            dt.Columns.Add(cl);
            cl = new DataColumn("Type", System.Type.GetType("System.String"));
            dt.Columns.Add(cl);
            cl = new DataColumn("Model", System.Type.GetType("System.String"));
            dt.Columns.Add(cl);
            cl = new DataColumn("Custodian", System.Type.GetType("System.String"));
            dt.Columns.Add(cl);
            cl = new DataColumn("StorageUnits", System.Type.GetType("System.String"));
            dt.Columns.Add(cl);
            cl = new DataColumn("Money", System.Type.GetType("System.String"));
            dt.Columns.Add(cl);
            cl = new DataColumn("Status", System.Type.GetType("System.String"));
            dt.Columns.Add(cl);
            cl = new DataColumn("BuyTime", System.Type.GetType("System.String"));
            dt.Columns.Add(cl);
            cl = new DataColumn("StorageSites", System.Type.GetType("System.String"));
            dt.Columns.Add(cl);
            cl = new DataColumn("FinancialNumbers", System.Type.GetType("System.String"));
            dt.Columns.Add(cl);
            cl = new DataColumn("Remark", System.Type.GetType("System.String"));
            dt.Columns.Add(cl);  
            for (int i = 2; i < sheet.Rows.Count; i++)
            {
                DataRow row = dt.NewRow();
                row[0] = sheet.Rows[ushort.Parse(i.ToString())].GetCell(1).Value;
                row[1] = sheet.Rows[ushort.Parse(i.ToString())].GetCell(2).Value;
                row[2] = sheet.Rows[ushort.Parse(i.ToString())].GetCell(3).Value;
                row[3] = sheet.Rows[ushort.Parse(i.ToString())].GetCell(4).Value;
                row[4] = sheet.Rows[ushort.Parse(i.ToString())].GetCell(5).Value;
                row[5] = sheet.Rows[ushort.Parse(i.ToString())].GetCell(6).Value;
                row[6] = sheet.Rows[ushort.Parse(i.ToString())].GetCell(7).Value;
                row[7] = sheet.Rows[ushort.Parse(i.ToString())].GetCell(8).Value;
                row[8] = sheet.Rows[ushort.Parse(i.ToString())].GetCell(9).Value;
                row[9] = sheet.Rows[ushort.Parse(i.ToString())].GetCell(10).Value;
                row[10] = sheet.Rows[ushort.Parse(i.ToString())].GetCell(11).Value;
                row[11] = sheet.Rows[ushort.Parse(i.ToString())].GetCell(12).Value;
                row[12] = sheet.Rows[ushort.Parse(i.ToString())].GetCell(13).Value;
                dt.Rows.Add(row);
            }
            return dt;
        }

        public double Str2Double(string str)
        {
            double num = 0;
            try
            {
                num = Convert.ToDouble(str);
            }
            catch
            { }
            return num;
        }

        public int Str2Int(string str)
        {
            int num = 0;
            try
            {
                num = Convert.ToInt32(str);
            }
            catch
            { }
            return num;
        }
    }
}

 

MyXLS案例

标签:

原文地址:http://www.cnblogs.com/varorbc/p/4214710.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!