你好我有這個代碼,我創建一個xlsx文件,我需要預先設置xlsx表格單元格的寬度。 實際的問題是,當我打開excell我需要用鼠標雙擊列之間的差距,以解開列和revieal隱藏的數據。 有沒有辦法用Epplus來做這個程序?如何在C#中使用EPPlus設置xlsx單元格寬度
using (ExcelPackage p = new ExcelPackage())
{
String filepath = "C://StatsYellowPages.csv";
DataSet ds = ExportCSVFileToDataset(filepath, "tblCustomers", "\t");
//Here setting some document properties
p.Workbook.Properties.Title = "StatsYellowPages";
//Create a sheet
p.Workbook.Worksheets.Add("Sample WorkSheet");
ExcelWorksheet ws = p.Workbook.Worksheets[1];
ws.Name = "StatsYellowPages"; //Setting Sheet's name
//Merging cells and create a center heading for out table
ws.Cells[1, 1].Value = "StatsYellowPages";
ws.Cells[1, 1, 1, ds.Tables[0].Columns.Count].Merge = true;
ws.Cells[1, 1, 1, ds.Tables[0].Columns.Count].Style.Font.Bold = true;
ws.Cells[1, 1, 1, ds.Tables[0].Columns.Count].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center;
int colIndex = 1;
int rowIndex = 2;
foreach (DataColumn dc in ds.Tables[0].Columns) //Creating Headings
{
var cell = ws.Cells[rowIndex, colIndex];
//Setting the background color of header cells to Gray
var fill = cell.Style.Fill;
fill.PatternType = ExcelFillStyle.Solid;
fill.BackgroundColor.SetColor(Color.Gray);
//Setting Top/left,right/bottom borders.
var border = cell.Style.Border;
border.Bottom.Style = ExcelBorderStyle.Thin;
border.Top.Style = ExcelBorderStyle.Thin;
border.Left.Style = ExcelBorderStyle.Thin;
border.Right.Style = ExcelBorderStyle.Thin;
//Setting Heading Value in cell
cell.Value = dc.ColumnName;
colIndex++;
}
foreach (DataRow dr in ds.Tables[0].Rows) // Adding Data into rows
{
colIndex = 1;
rowIndex++;
foreach (DataColumn dc in ds.Tables[0].Columns)
{
var cell = ws.Cells[rowIndex, colIndex];
//Setting Value in cell
cell.Value = dr[dc.ColumnName].ToString();
//Setting borders of cell
var border = cell.Style.Border;
colIndex++;
}
}
//Generate A File with Random name
Byte[] bin = p.GetAsByteArray();
string file = "c:\\StatsYellowPages.xlsx";
File.WriteAllBytes(file, bin);
感謝名單,似乎工作版本好。 – themis 2012-02-02 16:33:01
我會補充說,如果你想自動調整工作表中的所有列,請執行以下操作:for(i = 1; i <= ws.Dimension.End.Column; i ++){ws.Column(i).AutoFit ); }' – guanome 2013-05-22 13:25:33
它的工作原理,但設置不同的值,例如我想設置列的寬度爲7.86,但它設置爲7.14和3.5設置爲2.71 – 2013-07-28 08:15:27