通过C#代码对Excel进行操作,完成excel 工作簿(WorkBook)的新建,保存。对单元表(worksheet)内容填充,格式设置等。
调用Microsoft.Office.Interop.Excel,借助COM组件,与Excel进行互操作。
(来源于:https://www.cnblogs.com/zhangchenliang/archive/2012/02/24/2365956.html)
mysheet.Cells.Select();
mysheet.Cells.Columns.AutoFit();
excelRangeParm.Merge(Missing.Value);
excelRangeParm.Font.Bold = true;
excelRangeParm.Font.Size = 12;
excelRangeParm.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
excelRangeParm.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
excelRangeParm.FormulaR1C1 = 公式;
excelRange.ColumnWidth = 宽度;
excelRange.RowHeight = 行高;
Excel.Range myrange=mysheet.get_Range(mysheet.Cells[1,1],mysheet.Cells[5,1]);
myrange.NumberFormatLocal="@";
style.NumberFormatLocal = "[DBNum2][$-804]G/通用格式";
或
range.NumberFormatLocal = "G/通用格式";
xlsheet.Cells[1,1]="''+txtKey.Text;
((Excel.Range)mysheet.Cells[15,3]).EntireRow.Insert(0);
mysheet.get_Range((Excel.Range)mysheet.Cells[10,1], (Excel.Range)mysheet.Cells[10,200]).Select();
mysheet.get_Range((Excel.Range)mysheet.Cells[10,1], (Excel.Range)mysheet.Cells[10,200]).Interior.ColorIndex=3;
myrange.WrapText = true;
myrange.EntireRow.AutoFit();
补充:
Excel.Range range = xSheet.Range[xSheet.Cells[1, 1], xSheet.Cells[1, columnNum]];
using Excel = Microsoft.Office.Interop.Excel;
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string[] wordLines= File.ReadAllLines("../../1.txt",Encoding.Default);
//因为一个作者有多部作品所以进行分组
List list = new List();
foreach (var item in wordLines)
{
string[] infos = item.Split(' ');
if (infos.Length < 2) continue;
list.Add(new AuthorInfo { Author = infos[1], Production = infos[0] });
}
//一个作者可能存在多部作品所以进行分组
var arr = list.GroupBy(info => info.Author);
Excel.Application app = new Excel.Application();
app.DisplayAlerts = false;
//创建一个工作簿
Excel.Workbook book= app.Workbooks.Add();
foreach (var g in arr)
{
Excel.Worksheet sheet= book.Worksheets.Add();
sheet.Name = g.Key;
int num = 1;
foreach (var item in g)
{
//给sheet单元格设置值
//sheet.Range[sheet.Cells[1, 1],sheet.Cells[num,1]]必须以此格式设置否则报错
Excel.Range range= sheet.Range[sheet.Cells[num, 1], sheet.Cells[num, 1]] as Excel.Range;
//设置单元格格式
if (num % 2 == 1)
{
range.Font.Color = Color.Green;
range.Font.Bold = true;
range.Font.Size = 16;
//range.Font.Background = Color.SkyBlue;//不能设置
range.Interior.Color = Color.SkyBlue;
}
sheet.Cells[num, 1] = item.Production;
num++;
}
//单元表自动调整
sheet.Select();
sheet.Cells.Columns.AutoFit();
}
string filePath = Path.GetFullPath("../../1.xlsx");
book.SaveAs(filePath);
book.Close();
app.Quit();
//注意,进程并未退出而是一直存在,所以这里必须kill 进程
//获取app对应的进程ID
IntPtr hwd = new IntPtr(app.Hwnd);
int id;
GetWindowThreadProcessId(hwd, out id);
//kill该进程
System.Diagnostics.Process.GetProcessById(id).Kill();
MessageBox.Show("已导出!");
}
[DllImport("User32.dll", CharSet = CharSet.Auto)] //调用API函数,目的获取新增EXCEL进程的进程ID
public static extern int GetWindowThreadProcessId(IntPtr hwnd, out int ID);
}
class AuthorInfo
{
public string Author { get; set; }
public string Production { get; set; }
}