最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

OpenXml讀寫(xiě)Excel實(shí)例代碼

 更新時(shí)間:2013年12月02日 14:50:47   作者:  
這篇文章主要介紹了OpenXml讀寫(xiě)Excel代碼分享,大家參考使用

新版本的xlsx是使用新的存儲(chǔ)格式,貌似是處理過(guò)的XML。

對(duì)于OpenXML我網(wǎng)上搜了一下,很多人沒(méi)有介紹。所以我就這里推薦下,相信會(huì)成為信息系統(tǒng)開(kāi)發(fā)的必備。

先寫(xiě)出個(gè)例子,會(huì)發(fā)現(xiàn)如此的簡(jiǎn)介:

復(fù)制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Text;
using XFormular.config;
using System.IO;
using com.xtar.amfx;
using System.Runtime.Serialization.Formatters.Binary;
using System.Data;

namespace XFormular.test
{
    class Class1
    {
        public void test()
        {
            DataTable table = new DataTable("1");
            table.Columns.Add("2");
            for (int i = 0; i < 10; i++)
            {
                DataRow row = table.NewRow();
                row[0] = i;
                table.Rows.Add(row);
            }

            List<DataTable> lsit = new List<DataTable>();
            lsit.Add(table);

            OpenXmlSDKExporter.Export(AppDomain.CurrentDomain.BaseDirectory + "\\excel.xlsx", lsit);
        }
    }
}

寫(xiě)出代碼

復(fù)制代碼 代碼如下:

using System;
using System.IO;
using System.Windows.Forms;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using DocumentFormat.OpenXml.Extensions;
using System.Collections.Generic;
using System.Data;
using System.Text.RegularExpressions;

namespace XFormular
{
    class OpenXmlSDKExporter
    {
        private static string[] Level = {"A", "B", "C", "D", "E", "F", "G",
    "H", "I", "G", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
    "U", "V", "W", "X", "Y", "Z" };

        public static List<DataTable> Import(string path)
        {
            List<DataTable> tables = new List<DataTable>();

            if (path.EndsWith(ExcelHelper.POSTFIX_SVN))
                return tables;

            using (MemoryStream stream = SpreadsheetReader.StreamFromFile(path))
            {
                using (SpreadsheetDocument doc = SpreadsheetDocument.Open(stream, true))
                {
                    foreach (Sheet sheet in doc.WorkbookPart.Workbook.Descendants<Sheet>())
                    {
                        DataTable table = new DataTable(sheet.Name.Value);

                        WorksheetPart worksheet = (WorksheetPart)doc.WorkbookPart.GetPartById(sheet.Id);

                        List<string> columnsNames = new List<string>();

                        foreach (Row row in worksheet.Worksheet.Descendants<Row>())
                        {
                            foreach (Cell cell in row)
                            {
                                string columnName = Regex.Match(cell.CellReference.Value, "[a-zA-Z]+").Value;

                                if (!columnsNames.Contains(columnName))
                                {
                                    columnsNames.Add(columnName);
                                }

                            }
                        }

                        columnsNames.Sort(CompareColumn);

                        foreach (string columnName in columnsNames)
                        {
                            table.Columns.Add(columnName);
                        }

                        foreach (Row row in worksheet.Worksheet.Descendants<Row>())
                        {
                            DataRow tableRow = table.NewRow();
                            table.Rows.Add(tableRow);

                            foreach (Cell cell in row)
                            {
                                string columnName = Regex.Match(cell.CellReference.Value, "[a-zA-Z]+").Value;
                                tableRow[columnName] = GetValue(cell, doc.WorkbookPart.SharedStringTablePart);
                            }
                        }

                        if (table.Rows.Count <= 0)
                            continue;
                        if (table.Columns.Count <= 0)
                            continue;

                        tables.Add(table);
                    }
                }
            }

            return tables;
        }

        public static String GetValue(Cell cell, SharedStringTablePart stringTablePart)
        {

            if (cell.ChildElements.Count == 0)

                return null;

            //get cell value

            String value = cell.CellValue.InnerText;

            //Look up real value from shared string table

            if ((cell.DataType != null) && (cell.DataType == CellValues.SharedString))

                value = stringTablePart.SharedStringTable

                .ChildElements[Int32.Parse(value)]

                .InnerText;

            return value;

        }


        public static void Export(string path, List<DataTable> tables)
        {
            using (MemoryStream stream = SpreadsheetReader.Create())
            {
                using (SpreadsheetDocument doc = SpreadsheetDocument.Open(stream, true))
                {
                    SpreadsheetWriter.RemoveWorksheet(doc, "Sheet1");
                    SpreadsheetWriter.RemoveWorksheet(doc, "Sheet2");
                    SpreadsheetWriter.RemoveWorksheet(doc, "Sheet3");

                    foreach (DataTable table in tables)
                    {
                        WorksheetPart sheet = SpreadsheetWriter.InsertWorksheet(doc, table.TableName);
                        WorksheetWriter writer = new WorksheetWriter(doc, sheet);

                        SpreadsheetStyle style = SpreadsheetStyle.GetDefault(doc);

                        foreach (DataRow row in table.Rows)
                        {
                            for (int i = 0; i < table.Columns.Count; i++)
                            {
                                string columnName = SpreadsheetReader.GetColumnName("A", i);
                                string location = columnName + (table.Rows.IndexOf(row) + 1);
                                writer.PasteText(location, row[i].ToString(), style);
                            }
                        }

                        writer.Save();
                    }
                    SpreadsheetWriter.StreamToFile(path, stream);//保存到文件中
                }
            }
        }

        private static int CompareColumn(string x, string y)
        {
            int xIndex = Letter_to_num(x);
            int yIndex = Letter_to_num(y);
            return xIndex.CompareTo(yIndex);
        }

        /// <summary>
        /// 數(shù)字26進(jìn)制,轉(zhuǎn)換成字母,用遞歸算法
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private static string Num_to_letter(int value)
        {
            //此處判斷輸入的是否是正確的數(shù)字,略(正在表達(dá)式判斷)
            int remainder = value % 26;
            //remainder = (remainder == 0) ? 26 : remainder;
            int front = (value - remainder) / 26;
            if (front < 26)
            {
                return Level[front - 1] + Level[remainder];
            }
            else
            {
                return Num_to_letter(front) + Level[remainder];
            }
            //return "";
        }

        /// <summary>
        /// 26進(jìn)制字母轉(zhuǎn)換成數(shù)字
        /// </summary>
        /// <param name="letter"></param>
        /// <returns></returns>
        private static int Letter_to_num(string str)
        {
            //此處判斷是否是由A-Z字母組成的字符串,略(正在表達(dá)式片段)
            char[] letter = str.ToCharArray(); //拆分字符串
            int reNum = 0;
            int power = 1; //用于次方算值
            int times = 1;  //最高位需要加1
            int num = letter.Length;//得到字符串個(gè)數(shù)
            //得到最后一個(gè)字母的尾數(shù)值
            reNum += Char_num(letter[num - 1]);
            //得到除最后一個(gè)字母的所以值,多于兩位才執(zhí)行這個(gè)函數(shù)
            if (num >= 2)
            {
                for (int i = num - 1; i > 0; i--)
                {
                    power = 1;//致1,用于下一次循環(huán)使用次方計(jì)算
                    for (int j = 0; j < i; j++)           //冪,j次方,應(yīng)該有函數(shù)
                    {
                        power *= 26;
                    }
                    reNum += (power * (Char_num(letter[num - i - 1]) + times));  //最高位需要加1,中間位數(shù)不需要加一
                    times = 0;
                }
            }
            //Console.WriteLine(letter.Length);
            return reNum;
        }

        /// <summary>
        /// 輸入字符得到相應(yīng)的數(shù)字,這是最笨的方法,還可用ASIICK編碼;
        /// </summary>
        /// <param name="ch"></param>
        /// <returns></returns>
        private static int Char_num(char ch)
        {
            switch (ch)
            {
                case 'A':
                    return 0;
                case 'B':
                    return 1;
                case 'C':
                    return 2;
                case 'D':
                    return 3;
                case 'E':
                    return 4;
                case 'F':
                    return 5;
                case 'G':
                    return 6;
                case 'H':
                    return 7;
                case 'I':
                    return 8;
                case 'J':
                    return 9;
                case 'K':
                    return 10;
                case 'L':
                    return 11;
                case 'M':
                    return 12;
                case 'N':
                    return 13;
                case 'O':
                    return 14;
                case 'P':
                    return 15;
                case 'Q':
                    return 16;
                case 'R':
                    return 17;
                case 'S':
                    return 18;
                case 'T':
                    return 19;
                case 'U':
                    return 20;
                case 'V':
                    return 21;
                case 'W':
                    return 22;
                case 'X':
                    return 23;
                case 'Y':
                    return 24;
                case 'Z':
                    return 25;
            }
            return -1;
        }
    }
}

復(fù)制代碼 代碼如下:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace xtar_biz_codegen
{
    class ExcelHelper
    {
        public static string POSTFIX_97 = "XLS";

        public static string POSTFIX_03 = "XLSX";
    }
}

相關(guān)文章

  • C#中關(guān)于zip壓縮解壓幫助類的封裝 附源碼下載

    C#中關(guān)于zip壓縮解壓幫助類的封裝 附源碼下載

    之前一個(gè)同學(xué)問(wèn)了這個(gè)問(wèn)題后,看了園子里其它園友的封裝,都很零碎,調(diào)用也不是很方便。所以自己就封裝了一個(gè)zip解壓的類。后來(lái)想整理下怕自己忘了。就把壓縮的類也一并封裝了
    2013-02-02
  • des加密解密源碼 C# key值問(wèn)題分析

    des加密解密源碼 C# key值問(wèn)題分析

    本文主要介紹了des加密解密源碼,C# key值問(wèn)題,大家參考使用吧
    2014-01-01
  • 基于.net中突破每客戶端兩個(gè)http連接限制的詳細(xì)介紹

    基于.net中突破每客戶端兩個(gè)http連接限制的詳細(xì)介紹

    本篇文章是對(duì).net中突破每客戶端兩個(gè)http連接限制進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • 使用C#代碼在PDF文檔中添加、刪除和替換圖片

    使用C#代碼在PDF文檔中添加、刪除和替換圖片

    在當(dāng)今數(shù)字化文檔處理場(chǎng)景中,動(dòng)態(tài)操作PDF文檔中的圖像已成為企業(yè)級(jí)應(yīng)用開(kāi)發(fā)的核心需求之一,本文 將介紹如何在.NET平臺(tái)使用C#代碼在PDF文檔中添加、刪除和替換圖片,需要的朋友可以參考下
    2025-04-04
  • C#列表List<T>、HashSet和只讀集合介紹

    C#列表List<T>、HashSet和只讀集合介紹

    這篇文章介紹了C#中的列表List<T>、HashSet和只讀集合,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-05-05
  • C#目錄和文件管理操作詳解

    C#目錄和文件管理操作詳解

    在C#中常用的目錄操作類有Directory,DirectoryInfo,下面這篇文章主要給大家介紹了關(guān)于C#目錄和文件管理操作的相關(guān)資料,需要的朋友可以參考下
    2022-10-10
  • C#客戶端程序調(diào)用外部程序的3種實(shí)現(xiàn)方法

    C#客戶端程序調(diào)用外部程序的3種實(shí)現(xiàn)方法

    這篇文章主要給大家介紹了關(guān)于C#客戶端程序調(diào)用外部程序的3種實(shí)現(xiàn)方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-04-04
  • C#寫(xiě)日志類實(shí)例

    C#寫(xiě)日志類實(shí)例

    這篇文章主要介紹了C#寫(xiě)日志類,實(shí)現(xiàn)將日志信息寫(xiě)入文本文件的功能,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2014-10-10
  • C# 嵌入dll 的方法

    C# 嵌入dll 的方法

    這篇文章主要介紹了C# 嵌入dll 的方法,本文圖文并茂給大家及時(shí)的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2018-07-07
  • 探討:關(guān)閉瀏覽器后,php腳本會(huì)不會(huì)繼續(xù)運(yùn)行

    探討:關(guān)閉瀏覽器后,php腳本會(huì)不會(huì)繼續(xù)運(yùn)行

    本篇文章是對(duì)關(guān)閉瀏覽器后,php腳本會(huì)不會(huì)繼續(xù)運(yùn)行進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-06-06

最新評(píng)論

长岭县| 四子王旗| 金塔县| 南华县| 怀来县| 沅陵县| 大庆市| 兴仁县| 定日县| 察隅县| 抚州市| 海南省| 定日县| 黄冈市| 综艺| 德兴市| 双桥区| 广灵县| 呼图壁县| 乌拉特后旗| 高雄市| 阿鲁科尔沁旗| 上思县| 镇平县| 仪陇县| 兰坪| 辽中县| 南靖县| 子长县| 淮阳县| 扎兰屯市| 安福县| 德化县| 乌什县| 洪洞县| 昆山市| 嘉峪关市| 任丘市| 玛曲县| 鞍山市| 罗甸县|