OpenXml reads and writes Excel instance code

  • 2020-05-24 06:05:22
  • OfStack

The new version of xlsx USES the new storage format, which appears to be processed XML.

For OpenXML, I searched 1 time on the Internet, many people did not introduce it. So I recommend here, I believe it will become a necessary information system development.

Write an example first, and you'll find this brief introduction:


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);
        }
    }
}

Write the code


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);// Save to a file 
                }
            }
        }
        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>
        ///  digital 26 Base, convert to letters, use a recursive algorithm 
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        private static string Num_to_letter(int value)
        {
            // Here determine whether the input is the correct number, skip (judge in the expression) 
            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 Convert base letters into Numbers 
        /// </summary>
        /// <param name="letter"></param>
        /// <returns></returns>
        private static int Letter_to_num(string str)
        {
            // Here determine whether by A-Z A string of letters, abbreviated (in a fragment of an expression). 
            char[] letter = str.ToCharArray(); // Split string 
            int reNum = 0;
            int power = 1; // It's used to evaluate powers 
            int times = 1;  // The highest bit needs to be added 1
            int num = letter.Length;// Get the number of strings 
            // Get the final 1 The last value of a letter 
            reNum += Char_num(letter[num - 1]);
            // Get divided by the end 1 All the values of four letters , It takes more than two to execute this function 
            if (num >= 2)
            {
                for (int i = num - 1; i > 0; i--)
                {
                    power = 1;// to 1 For the 1 The second cycle is calculated using the power 
                    for (int j = 0; j < i; j++)           // The power, j To the power, there should be a function 
                    {
                        power *= 26;
                    }
                    reNum += (power * (Char_num(letter[num - i - 1]) + times));  // The highest bit needs to be added 1 You don't have to add the median 1
                    times = 0;
                }
            }
            //Console.WriteLine(letter.Length);
            return reNum;
        }
        /// <summary>
        ///  Enter a character to get the corresponding number, this is the most stupid method, still available ASIICK Coding; 
        /// </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;
        }
    }
}


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";
    }
}


Related articles: