[英]Auto width of column and word wrap of Headers not working in OpenXML Excel
當我嘗試使用 OpenXML 打開 excel 文件時。 Excel 運行良好,但我的樣式屬性沒有使用。
我需要給我的 excel 數據列提供自動寬度屬性。我找到了這個解決方案,但它對我不起作用
https://stackoverflow.com/questions/18268620/openxml-auto-size-column- width- in-excel
2.我無法對列標題進行自動換行,但值已被包裝
下面是我的 excel 代碼,它具有要設置的寬度屬性
public static string OpenXMLCreateXL(string FolderPath, DataSet tableSet)
{
WorkbookPart wBookPart = null;
var datetime = DateTime.Now.ToString().Replace("/", "_").Replace(":", "_");
string FilePath = "";
foreach (DataTable table1 in tableSet.Tables)
{
if (table1.Rows.Count != 0)
{
using (SpreadsheetDocument spreadsheetDoc = SpreadsheetDocument.Create(FilePath,
SpreadsheetDocumentType.Workbook))
{
wBookPart = spreadsheetDoc.AddWorkbookPart();
wBookPart.Workbook = new Workbook();
uint sheetId = 1;
spreadsheetDoc.WorkbookPart.Workbook.Sheets = new Sheets();
Sheets sheets = spreadsheetDoc.WorkbookPart.Workbook.GetFirstChild();
WorkbookStylesPart wbsp = wBookPart.AddNewPart();
wbsp.Stylesheet = CreateStylesheet();
wbsp.Stylesheet.Save();
foreach (DataTable table in tableSet.Tables)
{
if (table.Rows.Count != 0)
{
table.TableName = table.Rows[0]["LeaseCondition"].ToString();
WorksheetPart wSheetPart = wBookPart.AddNewPart();
Sheet sheet = new Sheet() { Id = spreadsheetDoc.WorkbookPart.GetIdOfPart(wSheetPart), SheetId =
sheetId, Name = table.TableName };
sheets.Append(sheet);
SheetData sheetData = new SheetData();
wSheetPart.Worksheet = new Worksheet();
Row headerRow = new Row();
Columns columns = new Columns();
int ColumnNumber = 1;
foreach (DataColumn column in table.Columns)
{
Cell cell = new Cell();
cell.DataType = CellValues.String;
cell.CellValue = new CellValue(column.ColumnName);
cell.StyleIndex = 2;
headerRow.AppendChild(cell);
Column column1 = new Column();
column1.Width = 30; ;
column1.BestFit = true;
column1.CustomWidth = true; //how i can set autowidth here?
column1.Min = Convert.ToUInt32(ColumnNumber);
column1.Max = Convert.ToUInt32(ColumnNumber);
columns.AppendChild(column1);
ColumnNumber = ColumnNumber + 1;
}
wSheetPart.Worksheet.AppendChild(columns);
sheetData.AppendChild(headerRow);
foreach (DataRow dr in table.Rows)
{
Row row = new Row();
foreach (DataColumn column in table.Columns)
{
Cell cell = new Cell();
cell.DataType = CellValues.String;
cell.CellValue = new CellValue(dr[column].ToString());
cell.StyleIndex = 1;
row.AppendChild(cell);
}
sheetData.AppendChild(row);
}
sheetId++;
wSheetPart.Worksheet.AppendChild(sheetData);
}
}
}
}
}
return FilePath;
}
我的自動換行代碼
Alignment alignment0 = new Alignment();
alignment0.WrapText = true;
alignment0.Vertical = VerticalAlignmentValues.Top;
CellFormats cellFormats = new CellFormats(
new CellFormat(new Alignment() { WrapText = true }), // default
new CellFormat { FontId = 0, FillId = 0, BorderId =
1,ApplyBorder = true, Alignment = alignment0, ApplyAlignment = true }, //
body
new CellFormat { FontId = 1, FillId = 2, BorderId = 1, ApplyFill
= true ,ApplyAlignment = true,} // header
);
非常感謝任何幫助或建議!
如果您在創建 OpenXml 文檔時遇到問題,那么Open XML SDK Productivity Tool會非常有用 - 它可以讓您看到您實際創建的內容,或者允許您檢查在 Excel(或其他合適的應用程序)中創建的文檔。
當我做同樣的工作時,最初讓我注意到的一點是 XML 中元素的順序很重要,請確保以正確的順序添加部分 - 如果有疑問,請查看工作示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.