public void Write2DAToExcel(string path) { var workbook = new XLWorkbook(); var worksheet = workbook.Worksheets.Add(export.ObjectName.Truncate(30)); //write labels for (int rowindex = 0; rowindex < RowNames.Count(); rowindex++) { worksheet.Cell(rowindex + 2, 1).Value = RowNames[rowindex]; } for (int colindex = 0; colindex < ColumnNames.Count(); colindex++) { worksheet.Cell(1, colindex + 2).Value = ColumnNames[colindex]; } //write data for (int rowindex = 0; rowindex < RowNames.Count(); rowindex++) { for (int colindex = 0; colindex < ColumnNames.Count(); colindex++) { if (Cells[rowindex, colindex] != null) { worksheet.Cell(rowindex + 2, colindex + 2).Value = Cells[rowindex, colindex].GetDisplayableValue(); } } } worksheet.SheetView.FreezeRows(1); worksheet.SheetView.FreezeColumns(1); worksheet.Columns().AdjustToContents(); workbook.SaveAs(path); }
/// <summary> /// 按照新的名称,排列组织数组 /// </summary> /// <param name="newRowNames"></param> /// <param name="defaultNewVal"></param> public void ArrangeRows(List <string> newRowNames, double defaultNewVal = 0) { //首先,在老矩阵中删除新矩阵没有的行或列 var rowNamesTobeRemoved = Geo.Utils.ListUtil.GetExcept <string>(RowNames, newRowNames); RemoveRow(rowNamesTobeRemoved); int irow = 0; foreach (var name in newRowNames) { if (RowNames.Contains(name))//若包含 { var index = (RowNames.IndexOf(name)); if (index != irow) //但非同一行 { ChangeRow(irow, index); //交换之 } } else { InsertRow(irow, name, defaultNewVal); } irow++; } }
/// <summary> /// Finds the name of each row, assuming at least one row exists in a table /// </summary> private void LoadRows() { do { // Count the number of rows in the table rows++; // Add the row name to the list, if name is missing, row is named after rank string title = iat.GetCellValue(iat.Part, title_row + rows, title_col); if (title == "") { title = rows.ToString(); } RowNames.Add(title); } // End of the table is marked by empty cells while (iat.GetCellValue(iat.Part, title_row + 1 + rows, title_col) != ""); return; }
public void Write2DAToExport() { using (var stream = new MemoryStream()) { //Cell count if (IsIndexed) { //Indexed ones seem to have 0 at start stream.WriteBytes(BitConverter.GetBytes(0)); } stream.WriteBytes(BitConverter.GetBytes(CellCount)); //Write cell data for (int rowindex = 0; rowindex < RowNames.Count(); rowindex++) { for (int colindex = 0; colindex < ColumnNames.Count(); colindex++) { Bio2DACell cell = Cells[rowindex, colindex]; if (cell != null) { if (IsIndexed) { //write index int index = (rowindex * ColumnNames.Count()) + colindex; //+1 because they are not zero based indexes since they are numerals stream.WriteBytes(BitConverter.GetBytes(index)); } stream.WriteByte(cell.Type); stream.WriteBytes(cell.Data); } else { if (IsIndexed) { //this is a blank cell. It is not present in the table. continue; } else { Console.WriteLine("THIS SHOULDN'T OCCUR!"); } } } } //Write Columns if (!IsIndexed) { stream.WriteBytes(BitConverter.GetBytes(0)); //seems to be a 0 before column definitions } //Console.WriteLine("Columns defs start at " + stream.Position.ToString("X6")); stream.WriteBytes(BitConverter.GetBytes(ColumnNames.Count())); for (int colindex = 0; colindex < ColumnNames.Count(); colindex++) { //Console.WriteLine("Writing column definition " + columnNames[colindex]); int nameIndexForCol = export.FileRef.findName(ColumnNames[colindex]); stream.WriteBytes(BitConverter.GetBytes(nameIndexForCol)); stream.WriteBytes(BitConverter.GetBytes(0)); //second half of name reference in 2da is always zero since they're always indexed at 0 stream.WriteBytes(BitConverter.GetBytes(colindex)); } int propsEnd = export.propsEnd(); byte[] binarydata = stream.ToArray(); byte[] propertydata = export.Data.Take(propsEnd).ToArray(); var newExportData = new byte[propertydata.Length + binarydata.Length]; propertydata.CopyTo(newExportData, 0); binarydata.CopyTo(newExportData, propertydata.Length); //Console.WriteLine("Old data size: " + export.Data.Length); //Console.WriteLine("NEw data size: " + newExportData.Length); if (export.Data.Length != newExportData.Length) { Console.WriteLine("FILES ARE WRONG SIZE"); Debugger.Break(); } export.Data = newExportData; } }
/// <summary> /// 移除行 /// </summary> /// <param name="index"></param> public void RemoveRow(int index) { Data.RemoveAt(index); RowNames.RemoveAt(index); }
/// <summary> /// 移除行 /// </summary> /// <param name="name"></param> public void RemoveRow(string name) { int index = RowNames.IndexOf(name); RemoveRow(index); }