下載位置
此程式需使用 “Aspose.Cells.dll”
需於編寫時 加入」References」(參考)
下載點
C# 部份
using Aspose.Cells;
String sError = string.Empty;
int titleColumnSpan = 0;//標題的跨列數
string path;//文件保存路徑
int columnIndex = 1;//列索引
int posStart = 0;//行索引
double dFee = 0;//費用(?)
try
{
//獲取保存路徑
path = @"C:\";
Workbook wb = new Workbook();
Worksheet ws = wb.Worksheets[0];
Cells cell = ws.Cells;
/*分析標題的跨列數*/
for (int i = 0; i < list.Columns.Count; i++)
{
if (list.Columns[i].Width > 0
&& list.Columns[i] != null && !list.Columns[i].Text.Trim().ToString().Equals("")
&& !list.Columns[i].Text.Trim().ToString().Equals("#"))
{
titleColumnSpan++;
}
}
//合併第一行單元格 -->存放標題
Range range = cell.CreateRange(posStart, 0, 1, titleColumnSpan);
range.Merge();
cell["A1"].PutValue(sTitleName);
posStart++;
////合併第二行單元格 -->存放時間起
//Range rangedtFr = cell.CreateRange(posStart, 0, 1, titleColumnSpan);
//rangedtFr.Merge();
//cell["A2"].PutValue("統計時間:" + sDateFr);
//posStart++;
////合併第三行單元格 -->存放時間止
//Range rangedtTo = cell.CreateRange(posStart, 0, 1, titleColumnSpan);
//rangedtTo.Merge();
//cell["A3"].PutValue("至:" + sDateTo);
//posStart++;
//設置行高
cell.SetRowHeight(0, 25);
//設置標題樣式
Style styleTitle = wb.Styles[wb.Styles.Add()];
styleTitle.HorizontalAlignment = TextAlignmentType.Center;//文字居中
styleTitle.Font.Name = "Segoe UI";
styleTitle.Font.IsBold = true;//設置粗體
styleTitle.Font.Size = 15;//設置字體大小
//設置列名樣式
Style styleColumn = wb.Styles[wb.Styles.Add()];
styleColumn.HorizontalAlignment = TextAlignmentType.Center;//文字居中
styleColumn.Font.Name = "Segoe UI";
styleColumn.Font.IsBold = true;//設置粗體
styleColumn.Font.Size = 11;//設置字體大小
//設置内容樣式
Style styleContent = wb.Styles[wb.Styles.Add()];
styleContent.HorizontalAlignment = TextAlignmentType.Center;
styleContent.Font.Size = 11;
//設置靠左樣式
Style styleLeft = wb.Styles[wb.Styles.Add()];
styleLeft.HorizontalAlignment = TextAlignmentType.Left;
styleLeft.Font.Name = "Segoe UI";
styleLeft.Font.IsBold = true;//設置粗體
styleLeft.Font.Size = 11;
//設置靠右樣式
Style styleRight = wb.Styles[wb.Styles.Add()];
styleRight.HorizontalAlignment = TextAlignmentType.Right;
styleRight.Font.Name = "Segoe UI";
styleRight.Font.IsBold = true;//設置粗體
styleRight.Font.Size = 11;
//給單元格關聯樣式
cell["A1"].SetStyle(styleTitle);
cell["A2"].SetStyle(styleRight);
cell["A3"].SetStyle(styleRight);
//設置Execl列名
columnIndex = 0;
for (int i = 0; i < list.Columns.Count; i++)
{
if (list.Columns[i].Width > 0
&& list.Columns[i] != null && !list.Columns[i].Text.Trim().ToString().Equals("")
&& !list.Columns[i].Text.Trim().ToString().Equals("#"))//當前列是否隱藏
{
cell[posStart, columnIndex].PutValue(list.Columns[i].Text);
cell[posStart, columnIndex].SetStyle(styleColumn);
columnIndex++;
}
}
posStart++;
//設置單元格内容
for (int i = 0; i < list.Items.Count; i++)
{
columnIndex = 0;
for (int j = 0; j < list.Columns.Count; j++)
{
if (list.Columns[j].Width > 0
&& list.Columns[j] != null && !list.Columns[j].Text.Trim().ToString().Equals("")
&& !list.Columns[j].Text.Trim().ToString().Equals("#"))//當前列是否隱藏
{
cell[i + posStart, columnIndex].PutValue(list.Items[i].SubItems[j].Text.ToString());
cell[i + posStart, columnIndex].SetStyle(styleContent);
columnIndex++;
if (list.Columns[j].Name == "fee")
{
dFee += double.Parse(list.Items[i].SubItems[j] == null ? "0" : list.Items[i].SubItems[j].Text.ToString());
}
}
}
}
posStart += list.Items.Count;
//設置列寬
columnIndex = 0;
for (int i = 0; i < list.Columns.Count; i++)
{
if (list.Columns[i].Width > 0
&& list.Columns[i] != null && !list.Columns[i].Text.Trim().ToString().Equals("")
&& !list.Columns[i].Text.Trim().ToString().Equals("#"))//當前列是否隱藏
{
cell.SetColumnWidth(columnIndex, list.Columns[i].Width / 7);
columnIndex++;
}
}
//統計欄
//合併單元格 -->存統計數據
//Range rangeStatistics = cell.CreateRange(posStart, 0, 1, titleColumnSpan);
//rangeStatistics.Merge();
//if (dFee > 0)
//{
// cell[posStart, 0].PutValue("合計:" + list.Items.Count + "人," + dFee + "元");
//}
//else
//{
// cell[posStart, 0].PutValue("合計:" + list.Items.Count + "人");
//}
//cell[posStart, 0].SetStyle(styleLeft);
posStart++;
//設置頁腳
//合併單元格 -->存制表人
//Range rangeUser = cell.CreateRange(posStart, 0, 1, titleColumnSpan);
//rangeUser.Merge();
//cell[posStart, 0].PutValue("制表人:" + sUser);
//cell[posStart, 0].SetStyle(styleRight);
posStart++;
//合併單元格 -->存制表時間
//Range rangeMakeTime = cell.CreateRange(posStart, 0, 1, titleColumnSpan);
//rangeMakeTime.Merge();
//cell[posStart, 0].PutValue("制表時間:" + DateTime.Now.ToString("yyyy年MM月dd日"));
//cell[posStart, 0].SetStyle(styleRight);
posStart++;
//保存excel表格
wb.Save(path);
}
}
catch (Exception ex)
{
//LoggerBiovision.LogException("", ex, "Excel導出發生異常,異常為:" + ex.ToString());
MessageBox.Show(ex.ToString());
return false;
}