public class ReportSummary
{
public void CreateHeader()
{
var header = new List<KeyValuePair<string, string>>
{
new KeyValuePair<string, string>("Description", "Summary By Role*"),
new KeyValuePair<string, string>("ColName1" , "Public"),
new KeyValuePair<string, string>("ColName2" , "Non Public"),
new KeyValuePair<string, string>("ColName3" , "Public"),
new KeyValuePair<string, string>("ColName4" , "Non Public"),
new KeyValuePair<string, string>("ColName5" , "Public"),
new KeyValuePair<string, string>("ColName6" , "Non Public"),
new KeyValuePair<string, string>("ColName7" , "Public"),
new KeyValuePair<string, string>("ColName8" , "Non Public"),
new KeyValuePair<string, string>("ColName9" , "Public"),
new KeyValuePair<string, string>("ColName10" , "Non Public"),
new KeyValuePair<string, string>("ColName11" , "Public"),
new KeyValuePair<string, string>("ColName12" , "Non Public")
};
for (int index = 0; index < header.Count; index++)
{
row[index] = header[index].Value;
}
AddRow(_currentRow, row, rowStyleIndex);
}
void AddRow(int rowIndex, object[] cells, params int[] styleIndexes)
{
if (cells != null)
{
for (int columnIndex = 0; columnIndex < cells.Length; columnIndex++)
{
int styleIndex = 16;
//If style idex is defined, pass it, or else pass the default style index 16, which is a bold header with white background
if (styleIndexes != null)
{
styleIndex = styleIndexes[columnIndex];
}
AddCell((uint)rowIndex, columnIndex + 1, cells[columnIndex], styleIndex);
}
}
}
}`
class ExcelProcessor:
public class ExcelProcessor2010
{
protected Cell AddCell(uint rowIndex, int columnIndex, object cellValue, int styleIndex)
{
//Removing the values for the Greyed out cell and Empty cell
if (string.IsNullOrEmpty(Convert.ToString(cellValue)) || styleIndex == 33)
{
cellValue = " ";
}
Cell cell = AddCell(rowIndex, columnIndex, cellValue);
cell.StyleIndex = (uint)styleIndex;
return cell;
}
protected Cell AddCell(uint rowIndex, int columnIndex, object cellValue)
{
Cell cell = null;
if (cellValue != null)
{
string cellValueAsString = cellValue.ToString();
if (!string.IsNullOrEmpty(cellValueAsString))
{
cell = InsertCellInWorksheet(GetColumnName(columnIndex), rowIndex);
if ((cellValue is bool || cellValue is bool?))
{
cell.DataType = CellValues.Boolean;
}
else if (cellValue is DateTime || cellValue is DateTime?)
{
cell.DataType = CellValues.Date;
}
else if (cellValue is string && !string.IsNullOrEmpty((string)cellValue))
{
cell.DataType = CellValues.SharedString;
}
else if (cellValue is byte || cellValue is byte? ||
cellValue is sbyte || cellValue is sbyte? ||
cellValue is short || cellValue is short? ||
cellValue is ushort || cellValue is ushort? ||
cellValue is int || cellValue is int? ||
cellValue is uint || cellValue is uint? ||
cellValue is long || cellValue is long? ||
cellValue is ulong || cellValue is ulong? ||
cellValue is float || cellValue is float? ||
cellValue is double || cellValue is double? ||
cellValue is decimal || cellValue is decimal?
)
{
cell.DataType = CellValues.Number;
}
if (cell.CellValue == null)
{
cell.Append(new CellValue
{
Text =
cell.DataType != CellValues.SharedString
? cellValueAsString
: InsertSharedStringItem(cellValueAsString)
});
}
else
{
cell.CellValue.Text = cellValueAsString;
}
}
}
return cell;
}
protected string InsertSharedStringItem(string text)
{
// If the part does not contain a SharedStringTable, create one.
if (SharedStringTablePart.SharedStringTable == null)
{
SharedStringTablePart.SharedStringTable = new SharedStringTable();
}
int i = 0;
// Iterate through all the items in the SharedStringTable. If the text already exists, return its index.
foreach (SharedStringItem item in SharedStringTablePart.SharedStringTable.Elements<SharedStringItem>())
{
if (item.InnerText == text)
{
return i.ToString();
}
i++;
}
// The text does not exist in the part. Create the SharedStringItem and return its index.
SharedStringTablePart.SharedStringTable.AppendChild(new SharedStringItem(new Text(text)));
//SharedStringTablePart.SharedStringTable.Save();
return i.ToString();
}
}
我正在使用OpenXML生成Excel文件。我正在尝试在Excel中创建一个表,如图所示:
但是它将列标题从Public / NonPublic更改为Public1,NonPublic2,Public3,NonPublic4等...经过一些研究了解到Excel需要唯一的列标题。但只有在使用OpenXML创建Excel时才会发生这种情况。如果您手动创建Excel并设置这些标题,那么它可以正常工作。
有人可以帮我吗?