aspose.cell制作excel常见写法

//设置Excel的基本格式信息

                Workbook workbook = new Workbook();

                Worksheet worksheet = workbook.Worksheets[0];

                Style styleHeader = workbook.Styles[workbook.Styles.Add()];

                styleHeader.Font.Size = 12;

                styleHeader.HorizontalAlignment = TextAlignmentType.Center;

                Style stylePowerInfo = workbook.Styles[workbook.Styles.Add()];

                stylePowerInfo.IsTextWrapped = true;

                Style styleTextCenter = workbook.Styles[workbook.Styles.Add()];

                styleTextCenter.HorizontalAlignment = TextAlignmentType.Center;

                Style styleTitle = workbook.Styles[workbook.Styles.Add()];

                styleTitle.Font.IsBold = true;

                styleTitle.Font.Size = 16;

                styleTitle.HorizontalAlignment = TextAlignmentType.Center;

                styleTitle.ForegroundColor = Color.LightGreen;

                //设置标题

                worksheet.Cells[0, 0].Value = "本类别资料";

                worksheet.Cells.Merge(0, 0, 1, 2);

                worksheet.Cells[0, 0].SetStyle(styleTitle);

                worksheet.Cells[1, 0].Value = "名称";

                worksheet.Cells[1, 0].SetStyle(styleTitle);

                worksheet.Cells[1, 1].Value = "代号";

                worksheet.Cells[1, 1].SetStyle(styleTitle);
//i+2表示从去掉总标题和子标题以后开始导出

                for (int i = 0; i < Convert.ToInt32(param[0]); i++)

                {

                    try

                    {

                        if (this.InvokeRequired)

                        {



                            worksheet.Cells[i + 2, 0].PutValue(GetListText(i, 2));

                            worksheet.Cells[i + 2, 0].SetStyle(styleTextCenter);

                            worksheet.Cells[i + 2, 1].PutValue(GetListText(i, 3));

                            worksheet.Cells[i + 2, 1].SetStyle(styleTextCenter);

                        }

                        else

                        {

                            worksheet.Cells[i + 2, 0].PutValue(this.listView.Items[i].SubItems[2].Text);

                            worksheet.Cells[i + 2, 0].SetStyle(styleTextCenter);

                            worksheet.Cells[i + 2, 1].PutValue(this.listView.Items[i].SubItems[3].Text);

                            worksheet.Cells[i + 2, 1].SetStyle(styleTextCenter);

                        }

                        if (progressBarX1.InvokeRequired)

                        {

                            progressBarX1.Invoke(new Action<int>(s => { this.progressBarX1.Value = s; }), i);

                        }

                        else

                        {

                            this.progressBarX1.Value = i;

                        }

                        if (lbState.InvokeRequired)

                        {

                            lbState.Invoke(new Action<string>(s => { this.lbState.Text = s; }), "当前执行第" + i + "条数据/共" + (this.listView.Items.Count - 1).ToString() + "条数据,请等待...");

                        

                        }

                        else

                        {

                            this.lbState.Text = "当前执行第" + i + "条数据/共" + (this.listView.Items.Count - 1).ToString() + "条数据,请等待...";

                        }



                    }

                    catch (Exception ex)

                    {

                        isSuccessful = false;

                        WriteLog(ex, "数据异常", false);

                        continue;

                    }

                }

                //设置列的宽度

                worksheet.Cells.SetColumnWidthPixel(0, 150);

                worksheet.Cells.SetColumnWidthPixel(1, 150);

                workbook.Save(FileFullName);
 //合并单元格

worksheet.Cells.Merge(rowIndex - userRowSpan, 0, userRowSpan, 1);

以上以从listview导出数据到excel为例

你可能感兴趣的:(Excel)