解决思路:
1、找个可用的Aspose.Cells(有钱还是买个正版吧,谁开发个东西也不容易);
2、在.Net方案中引用此Cells;
3、写个函数ToExcel(传递一个DataTable),可以另写个SetCellStyle的函数专门给各列设置样式。
4、在按钮的Click事件中调用此ToExcel()即可。
注:想更详细了解的到Aspose网站去,那里有很多在线文档,够你看的了。也可以下载个Demo程序研究。
部分代码贴在这里,代码还有需要改进的地方,暂时这么用着,给自己以后方便查找,再者给真正接触此控件的同志们抛个砖头:
ExportToExcel()的:
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- public bool ExportToExcel(System.Web.HttpResponse response,DataTable dt, string FileName, string SheetName, string Title,
- ArrayList ColTitle, ArrayList ColName, ArrayList ColWidth, ArrayList ColStyle, int ColTitleRow, ref string err)
- {
-
-
- if (ColTitle.Count != ColName.Count || ColTitle.Count != ColWidth.Count || ColTitle.Count != ColStyle.Count)
- {
- err = "数据组个数不一致";
- return false;
- }
-
- try
- {
-
- Workbook workbook = new Workbook();
-
-
-
-
-
-
-
-
- workbook.Worksheets.Clear();
- Worksheet worksheet = workbook.Worksheets.Add(SheetName);
- worksheet = workbook.Worksheets[0];
-
- Cells cells = worksheet.Cells;
-
-
-
- ArrayList styles = new ArrayList();
- styles = SetCellStyle(workbook, ColStyle);
-
-
- Range w;
- if(ColTitleRow>0)
- {
-
- cells[0,0].PutValue(Title);
-
- cells.SetRowHeight(0, 35);
-
- cells.Merge(0,0,1,(byte)ColName.Count);
-
- w=cells.CreateRange(0,0,1,(byte)ColName.Count);
-
-
- w.Style = workbook.Styles["SheetTitle"];
- }
-
-
- int currow = ColTitleRow;
- byte curcol = 0;
- foreach(string s in ColTitle)
- {
- cells[currow,curcol++].PutValue(s);
- cells.SetRowHeight(ColTitleRow, 25);
- }
-
- w=cells.CreateRange(currow,0,1,ColName.Count);
- w.Style = (Aspose.Cells.Style)styles[ColStyle.Count+1];
-
-
-
- currow++;
-
-
-
- for(int i=0; i<dt.Rows.Count; i++)
- {
- curcol = 0;
- for(int j=0; j<ColName.Count; j++)
- {
- object val = dt.Rows[i][ColName[j].ToString()].ToString().Trim();
- switch (int.Parse(ColStyle[j].ToString()))
- {
- case 4:
- if (val.ToString().Length>0)
- val = Int32.Parse(val.ToString());
- else
- val = "";
- break;
- case 5:
- if (val.ToString().Length>0)
- val = Decimal.Parse(val.ToString());
- else
- val = "";
- break;
- case 6:
- if (val.ToString().Length>0)
- val = DateTime.Parse(val.ToString());
- else
- val = "";
- break;
- case 7:
- if (val.ToString().Length>0)
- val = Decimal.Parse(val.ToString());
- else
- val = "";
- break;
- default:
- break;
- }
- cells[currow,curcol++].PutValue(val);
- }
- currow ++;
- }
- curcol = 0;
-
- for(int i=0; i<dt.Columns.Count; i++)
- {
- w = cells.CreateRange(ColTitleRow+1, i, dt.Rows.Count, 1);
- w.Style = (Aspose.Cells.Style)styles[i];
- }
-
-
-
-
- foreach(int s in ColWidth)
- cells.SetColumnWidth(curcol++, s);
-
-
-
-
- workbook.Save(FileName, FileFormatType.Default, SaveType.OpenInExcel, response);
-
- return true;
- }
- catch (Exception ex)
- {
- err = ex.Message;
- return false;
- }
- }
- #endregion
样式设置SetStyle()的:
- #region SetCellStyle()设置格式,如果需要增加新的格式,请在case后面增加,不要修改前面的
- public ArrayList SetCellStyle(Workbook workbook, ArrayList styleindex)
- {
-
- ArrayList CellStyle = new ArrayList(styleindex.Count + 2);
- Aspose.Cells.Style style = null;
- for (int i=0; i<styleindex.Count; i++)
- {
- int index = workbook.Styles.Add();
-
- style = workbook.Styles[index];
- style.Name = "Custom_Style" + ((int)(i + 1)).ToString();
- style.ForegroundColor = Color.White;
- style.HorizontalAlignment = TextAlignmentType.Center;
- style.VerticalAlignment = TextAlignmentType.Center;
- style.Font.Name = "宋体";
- style.Font.Size = 10;
- style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
- style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
- style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
- style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
-
- switch((int)styleindex[i])
- {
- case 1:
- style.HorizontalAlignment = TextAlignmentType.Center;
- style.VerticalAlignment = TextAlignmentType.Center;
- break;
- case 2:
- style.Font.Name = "Georgia";
- break;
- case 3:
- style.HorizontalAlignment = TextAlignmentType.Left;
- break;
- case 4:
- style.HorizontalAlignment = TextAlignmentType.Center;
- style.Number = 1;
- break;
- case 5:
- style.HorizontalAlignment = TextAlignmentType.Right;
- style.Number = 40;
- break;
- case 6:
- style.HorizontalAlignment = TextAlignmentType.Center;
- style.Number = 14;
- break;
- case 7:
- style.HorizontalAlignment = TextAlignmentType.Center;
- style.Number = 10;
- break;
- default:
- break;
- }
- CellStyle.Add(style);
- }
-
-
- int sindex = workbook.Styles.Add();
- style = workbook.Styles[sindex];
- style.Name = "SheetTitle";
- style.Font.Size = 14;
- style.Font.IsBold = true;
- style.Font.Name = "楷体_GB2312";
- style.HorizontalAlignment = TextAlignmentType.Center;
- style.VerticalAlignment = TextAlignmentType.Center;
- style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
- style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
- style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
- style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
-
- CellStyle.Add(style);
-
-
- sindex = workbook.Styles.Add();
- style = workbook.Styles[sindex];
- style.Name = "ColTitle";
- style.Font.Size = 12;
- style.Font.IsBold = true;
- style.Font.Name = "宋体";
- style.HorizontalAlignment = TextAlignmentType.Center;
- style.VerticalAlignment = TextAlignmentType.Center;
- style.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
- style.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
- style.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
- style.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
-
- CellStyle.Add(style);
-
-
- return CellStyle;
-
- }
- #endregion
|
请发表评论