日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Openxml: 导出excel 设置 cell的格式

發布時間:2025/6/17 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Openxml: 导出excel 设置 cell的格式 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

在cell中如果cell中的文本有換行符, 默認是不顯示換行的, 只有點了excel 工具欄中的“Wrap Text" 按鈕, 才會顯示換行, 見下圖:

?

這個效果, 可以通過設置openxml的 style sheet 來實現。

?

xml
??<?xml?version="1.0"?encoding="utf-8"??>?
<x:styleSheet?xmlns:x="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
<x:fonts?count="1">
?
<x:font>
??
<x:sz?val="11"?/>?
??
<x:color?theme="1"?/>?
??
<x:name?val="Calibri"?/>?
??
<x:family?val="2"?/>?
??
<x:scheme?val="minor"?/>?
??
</x:font>
??
</x:fonts>
<x:fills?count="2">
?
<x:fill>
??
<x:patternFill?patternType="none"?/>?
??
</x:fill>
??
</x:fills>
?
<x:borders?count="1">
?
<x:border>
??
<x:left?/>?
??
<x:right?/>?
??
<x:top?/>?
??
<x:bottom?/>?
??
<x:diagonal?/>?
??
</x:border>
??
</x:borders>
?
<x:cellXfs?count="2">
??
<x:xf?numFmtId="0"?fontId="0"?fillId="0"?borderId="0"?/>?
?
<x:xf?numFmtId="0"?fontId="0"?fillId="0"?borderId="0"?applyAlignment="1">
??
<x:alignment?wrapText="1"?/>?
??
</x:xf>
??
</x:cellXfs>
??
</x:styleSheet>

?

在創建stylesheet時, 必須創建fonts, Fills,Borders 和cellXfs(CellFormats)?四個節點,

在顯示cell是通過StyleIndex 來關聯 cellXfs的Index 來改變cell 的顯示樣式, 注意, 這個index只能從1 開始,因此需要在cellXfs中加兩個CellFormat子節點, 我們這里要設置 wrap text, 因此在第二個節點設置applyAlignment 并設wrap Text ="1". 上個關于openxml的帖子, 有人問怎么設置cell的 font,答案就是加一個font 子節點到fonts, 得到index, 再加一個cellformat 子節點 并設置fontid 為剛加的font的index。 把這個cellformat的id 給 要設置的cell的StyleIndex。

初始stylesheet 并加 wraptext? style:

Code
?private?void?InitializeStyleSheet()
????????
{
????????????spreadSheet.WorkbookPart.AddNewPart
<WorkbookStylesPart>();
????????????spreadSheet.WorkbookPart.WorkbookStylesPart.Stylesheet?
=?new?Stylesheet();
????????????Stylesheet?stylesheet?
=?spreadSheet.WorkbookPart.WorkbookStylesPart.Stylesheet;

????????????stylesheet.Fonts?
=?new?DocumentFormat.OpenXml.Spreadsheet.Fonts(new?Font(new?FontSize()?{?Val?=?11D?},?new?Color()?{?Theme?=?(UInt32Value)1U?},
????????????????????????????????????????
new?FontName()?{?Val?=?"Calibri"?},?new?FontFamily()?{?Val?=?2?},
????????????????????????????????????????
new?DocumentFormat.OpenXml.Spreadsheet.FontScheme()?{?Val?=?FontSchemeValues.Minor?}))?{?Count?=?(UInt32Value)1U?};
????????????stylesheet.Fills?
=?new?Fills(new?DocumentFormat.OpenXml.Spreadsheet.Fill(new?DocumentFormat.OpenXml.Spreadsheet.PatternFill()?{?PatternType?=?PatternValues.None?}))?{?Count?=?(UInt32Value)2U?};
????????????stylesheet.Borders?
=?new?Borders(new?Border(new?DocumentFormat.OpenXml.Spreadsheet.LeftBorder(),
????????????????
new?DocumentFormat.OpenXml.Spreadsheet.RightBorder(),?new?DocumentFormat.OpenXml.Spreadsheet.TopBorder(),
????????????????
new?DocumentFormat.OpenXml.Spreadsheet.BottomBorder(),?new?DiagonalBorder()))?{?Count?=?(UInt32Value)1U?};

????????????stylesheet.CellFormats?
=?new?CellFormats();
????????????stylesheet.CellFormats.Count?
=?2;
????????????CellFormat?cf0?
=?stylesheet.CellFormats.AppendChild(new?CellFormat());
????????????cf0.NumberFormatId?
=?0;
????????????cf0.FontId?
=?0;
????????????cf0.BorderId?
=?0;
????????????cf0.FillId?
=?0;
????????????CellFormat?cf?
=?stylesheet.CellFormats.AppendChild(new?CellFormat());
????????????cf.Alignment?
=?new?Alignment();
????????????cf.ApplyAlignment?
=?true;
????????????cf.NumberFormatId?
=?0;
????????????cf.FontId?
=?0;
????????????cf.BorderId?
=?0;
????????????cf.FillId?
=?0;
????????????cf.Alignment.WrapText?
=?true;
????????????spreadSheet.WorkbookPart.WorkbookStylesPart.Stylesheet.Save();
????????}

?

創建cell時, 對style的引用:

?

Code
???public?void?WriteNewCell(Row?row,?string?text,?string?cellName)
????????{
????????????
int?index?=?InsertSharedStringItem2(text,?shareStringPart);
????????????Cell?cell?
=?new?Cell()?{?CellReference?=?new?StringValue(cellName)?};
????????????cell.CellValue?
=?new?CellValue(index.ToString());
????????????cell.DataType?
=?new?EnumValue<CellValues>(CellValues.SharedString);
????????????cell.StyleIndex?
=?1;
????????????row.Append(cell);
????????}

?

注意 cell.StyleIndex = 1; 1 對應的是stylesheet 中的 cellfortmat 的index。

?

轉載于:https://www.cnblogs.com/skyfei/archive/2009/07/16/1524695.html

總結

以上是生活随笔為你收集整理的Openxml: 导出excel 设置 cell的格式的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。