1. 程式人生 > >Aspose.Cells匯入資料到Excel,支援單元格操作

Aspose.Cells匯入資料到Excel,支援單元格操作

public class AsposeExcelHelper
    {
        /// <summary>
        /// 操作例項
        /// </summary>
        private Workbook currentWorkbook;

        public AsposeExcelHelper()
        {
            License license = new License();
            license.SetLicense(@"E:\Demo\ExcelTools\ExcelTools\Excel\License.lic");//aspose需要license
            currentWorkbook = new Workbook();
            currentWorkbook.Worksheets.Clear();//預設會有一張sheet
        }

        /// <summary>
        /// 儲存
        /// </summary>
        public void Save()
        {
            string filePath = OpenSaveFolderBrowserDialog();
            currentWorkbook.Save(filePath);
        }

        /// <summary>
        /// 在Excel中新建sheet
        /// </summary>
        /// <param name="names"></param>
        public void CreateSheetByName(string[] names)
        {
            foreach (string name in names)
            {
                if (currentWorkbook.Worksheets.GetSheetByCodeName(name) == null)
                {
                    currentWorkbook.Worksheets.Add(name);

                    currentWorkbook.Worksheets.GetSheetByCodeName(name).AutoFitRows();
                }
            }
        }

        /// <summary>
        /// 向指定sheet中插入值
        /// </summary>
        public void ExportDataToExcel(string sheetName,List<string[]> values, int firstRow)
        {
            Cells cells = currentWorkbook.Worksheets.GetSheetByCodeName(sheetName).Cells;

            for(int valuesIndex = 0; valuesIndex < values.Count(); valuesIndex++)
            {
                string[] currentValue = values[valuesIndex];

                for (int valueIndex = 0; valueIndex < currentValue.Length; valueIndex++)
                {
                    cells[valuesIndex + firstRow, valueIndex].PutValue(currentValue[valueIndex]);
                }
            }
        }

        /// <summary>
        /// 合併指定sheet的指定單元格, 並插值(單行)
        /// </summary>
        /// <param name="numbers">合併單元格的個數</param>
        public void MergeCells(string sheetName,int[] numbers, int firstRow, string[] values)
        {
            Cells cells = currentWorkbook.Worksheets.GetSheetByCodeName(sheetName).Cells;
            int firstColumn = 0;
            for(int index = 0;index < numbers.Length;index ++ )
            {
                int number = numbers[index];

                cells.Merge(firstRow, firstColumn, 1, number);

                cells[firstRow, firstColumn].PutValue(values[index]);

                firstColumn += number;
            }
        }

        /// <summary>
        /// 設定儲存Excel的路徑
        /// </summary>
        private string OpenSaveFolderBrowserDialog()
        {
            SaveFileDialog fd = new SaveFileDialog();
            fd.Filter = "EXCEL檔案(*.xls)|*.xls|EXCEL檔案(*.xlsx)|*.xlsx";
            fd.FileName = "新建 Microsoft Excel 工作表";
            if (fd.ShowDialog() == DialogResult.OK)
            {
                return fd.FileName;
            }

            return string.Empty;
        }
    }

呼叫例子
private void BtnExport_Click(object sender, EventArgs e)
        {
            string sheetName = "IceWang";

            AsposeExcelHelper aspose = new AsposeExcelHelper();

            aspose.CreateSheetByName(new string[] { sheetName });

            //合併指定單元格,並向單行插值
            aspose.MergeCells(sheetName, new int[] { 2, 3, 1, 3 }, 0, new string[] { "上次監測時間", "2018/1/25", "本次監測時間", "2018/1/26" });
            aspose.MergeCells(sheetName, new int[] { 2, 1, 1, 1, 1, 1, 1, 1 }, 1, new string[] { "測試專案", "變化最大位置(點號)", "本次測值", "本次速率", "累計值", "預警值", "是否超過預警值", "備註" });

            //向為合併單元格插值
            List<string[]> values = new List<string[]>();
            values.Add(new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9" });
            values.Add(new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9" });
            values.Add(new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9" });
            values.Add(new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9" });
            aspose.ExportDataToExcel(sheetName, values, 2);

            aspose.Save();
        }