zhangyuekai
2024-07-01 0e3464b6adf776686e66bb3189441ab1a65a088e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
using Spire.Xls;
using Spire.Xls.Core;
using System;
using System.IO;
 
 
namespace HStation.RevitDev.RevitDataExport.Utility
{
    internal class ExcelHelper
    {
        private Workbook _workbook;
        private Worksheet _sheet;
 
        public ExcelHelper()
        {
            Workbook workbook = new Workbook();
            workbook.Worksheets.Clear();
            _workbook = workbook;
        }
 
        internal void SetCurrentSheet(string sheetName)
        {
            if (_workbook.Worksheets[sheetName] == null)
            {
                _sheet = _workbook.Worksheets.Add(sheetName);
            }
        }
 
        internal void SaveAs(string filePath)
        {
            _workbook.SaveToFile(filePath);
        }
 
        internal void Write(int rowIndex, int columnIndex, string content)
        {
            _sheet.Range[rowIndex, columnIndex].Value = content;
        }
 
        private static char[] InvalidChars = { ':', ':', '\\', '/', '?', '?', '【', '[', ']', '】', '*' };
        public static string UniformSheetName(string name)
        {
            foreach (var invalidChar in InvalidChars)
            {
                name = name.Replace(invalidChar, '-');
            }
            return name;
        }
    }
}