Xin chào Tôi có mã này nơi tôi tạo tệp xlsx và tôi cần đặt trước chiều rộng của ô xlsx. Vấn đề thực tế là khi tôi mở excell tôi cần phải bấm đúp vào khoảng cách giữa các cột với con chuột để unwrap các cột và revieal dữ liệu được ẩn. Có cách nào để thực hiện chương trình này với Epplus không?Cách đặt chiều rộng ô xlsx bằng EPPlus trong C#
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);
thanx, có vẻ như để làm việc ver tốt . – themis
Tôi sẽ thêm rằng nếu bạn muốn tự động điền tất cả các cột trong một trang tính, hãy làm điều này 'cho (i = 1; i <= ws.Dimension.End.Column; i ++) { \t ws.Column (i) .AutoFit (); } ' – guanome
nó hoạt động nhưng thiết lập giá trị khác nhau ví dụ tôi muốn đặt chiều rộng cột thành 7.86 nhưng nó được đặt thành 7.14 và 3.5 nó được đặt thành 2.71 –