| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- /* ==============================================================================
- * 功能描述:NpoiHelper
- * 创 建 者:SAGACLOUD
- * 创建日期:2017/8/22 9:28:17
- * ==============================================================================*/
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Autodesk.Revit.DB;
- using Microsoft.Win32;
- using NPOI.SS.UserModel;
- using NPOI.XSSF.UserModel;
- using CellType = NPOI.SS.UserModel.CellType;
- namespace SpacePlugin.CobineWall
- {
- /// <summary>
- /// NpoiHelper
- /// </summary>
- class NpoiHelper
- {
- /// <summary>
- /// 读取excel新创建的虚拟墙信息
- /// </summary>
- /// <param name="z"></param>
- /// <returns></returns>
- public static List<KeyValuePair<XYZ, XYZ>> ReadExcelXyzs(string path,double z)
- {
- List<KeyValuePair<XYZ, XYZ>> pairs = new List<KeyValuePair<XYZ, XYZ>>();
- using (var fp = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
- {
- XSSFWorkbook book = new XSSFWorkbook(fp);
- var sheet = book.GetSheetAt(0);
- for (int i = 1; i <= sheet.LastRowNum; i++)
- {
- var row = sheet.GetRow(i);
- var str1 = row.GetCell(1)?.ToString();
- var str2 = row.GetCell(2)?.ToString();
- var str3 = row.GetCell(3)?.ToString();
- var str4 = row.GetCell(4)?.ToString();
- if (String.IsNullOrEmpty(str1) || String.IsNullOrEmpty(str2) ||
- String.IsNullOrEmpty(str3) || String.IsNullOrEmpty(str4)) continue;
- var cell1 = Double.Parse(str1);
- var cell2 = Double.Parse(str2);
- var cell3 = Double.Parse(str3);
- var cell4 = Double.Parse(str4);
- XYZ start = new XYZ(cell1, cell2, z);
- XYZ end = new XYZ(cell3, cell4, z);
- KeyValuePair<XYZ, XYZ> pair = new KeyValuePair<XYZ, XYZ>(start, end);
- pairs.Add(pair);
- }
- }
- return pairs;
- }
- /// <summary>
- /// 导出数据
- /// </summary>
- /// <param name="xyzsList"></param>
- public static void ExportToExcel(List<List<XYZ>> xyzsList)
- {
- SaveFileDialog sflg=new SaveFileDialog();
- sflg.Filter = "Excel(*.xlsx)|*.xlsx";
- if (sflg.ShowDialog() != true) return;
- ExportToExcel(xyzsList, sflg.FileName, sflg.FilterIndex);
- }
- /// <summary>
- /// 导出数据到指定目录
- /// </summary>
- /// <param name="xyzsList"></param>
- /// <param name="fileName"></param>
- public static void ExportToExcel(List<List<XYZ>> xyzsList,string fileName,int filterIndex=0)
- {
- IWorkbook book = new XSSFWorkbook();
- ISheet sheet = book.CreateSheet("Summary");
- #region 添加表头
- IRow row = sheet.CreateRow(0);
- ICell cell0 = row.CreateCell(0);
- cell0.SetCellType(CellType.String);
- cell0.SetCellValue("Edge");
- ICell cell1 = row.CreateCell(1);
- cell1.SetCellType(CellType.String);
- cell1.SetCellValue("端点 X");
- ICell cell2 = row.CreateCell(2);
- cell2.SetCellType(CellType.String);
- cell2.SetCellValue("端点 Y");
- ICell cell3 = row.CreateCell(3);
- cell3.SetCellType(CellType.String);
- cell3.SetCellValue("起点 X");
- ICell cell4 = row.CreateCell(4);
- cell4.SetCellType(CellType.String);
- cell4.SetCellValue("起点 Y");
- ICell cell5 = row.CreateCell(5);
- cell5.SetCellType(CellType.String);
- cell5.SetCellValue("角度");
- #endregion
- #region 添加数据
- int index = 0;
- foreach (List<XYZ> xyzs in xyzsList)
- {
- int count = xyzs.Count;
- for (int i = 0; i < count - 1; i++)
- {
- XYZ xyz0 = xyzs[i];
- XYZ xyz1 = xyzs[i + 1];
- index++;
- row = sheet.CreateRow(index);
- ICell cell = row.CreateCell(0, CellType.Numeric);
- cell.SetCellValue(index);
- cell = row.CreateCell(1, CellType.Numeric);
- cell.SetCellValue(xyz1.X);
- cell = row.CreateCell(2, CellType.Numeric);
- cell.SetCellValue(xyz1.Y);
- cell = row.CreateCell(3, CellType.Numeric);
- cell.SetCellValue(xyz0.X);
- cell = row.CreateCell(4, CellType.Numeric);
- cell.SetCellValue(xyz0.Y);
- }
- }
- #endregion
- #region 写入
- MemoryStream ms = new MemoryStream();
- book.Write(ms);
- book = null;
- using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
- {
- byte[] data = ms.ToArray();
- fs.Write(data, 0, data.Length);
- fs.Flush();
- }
- ms.Close();
- ms.Dispose();
- #endregion
- }
- }
- }
|