C1XLS写Excel文件

本文介绍使用ComponentOne的Excel组件进行Excel2003格式文件的基本功能测试过程,包括单元格样式设置、字体颜色调整、边框绘制及单元格合并等操作,并尝试保存为指定文件。

Component One的excel组件,使用2进制方式读写excel 2003格式

测试基本功能满足

'测试c1xls-merge cell ok,back color ok, font ok,border ok
Dim C1XLBook1 As New C1XLBook()
Dim sheet As XLSheet = C1XLBook1.Sheets(0)
Dim i As Integer
For i = 0 To 99
    sheet(i, 0).Value = i + 1
    Dim cs As New XLStyle(C1XLBook1)
[背景颜色]
    'cs.BackColor = Color.Blue
[字体]
    cs.Font = New Font("Tahoma", 9, FontStyle.Bold)
    cs.ForeColor = Color.Red
    'cs.BackPattern = XLPatternEnum.HorizontalStripe
[边界]
    cs.BorderBottom = XLLineStyleEnum.Thin
    cs.BorderLeft = XLLineStyleEnum.Thin
    cs.BorderRight = XLLineStyleEnum.Thin
    cs.BorderTop = XLLineStyleEnum.Thin
    sheet(i, 0).Style = cs
Next i
For i = 0 To 99 Step 2
    Dim cr As XLCellRange
    cr = New XLCellRange(i, i, 0, 1)
[合并单元格]
    sheet.MergedCells.Add(cr)
    Dim cs As New XLStyle(C1XLBook1)
    cs.SetBorderStyle(XLLineStyleEnum.Thin)
    'for normal style
    sheet(i, 0).Style = cs
    'for border
    sheet(i, 1).Style = cs
Next
' step 3: save the file
C1XLBook1.Save(Application.StartupPath & "/hello.xls")

 

另外,测试几个语法着色器,暂时都没用成功

C#可以使用Microsoft Office Interop库或者第三方库来读Excel文件1. 使用Microsoft Office Interop库读Excel文件 使用Microsoft Office Interop库需要先安装Microsoft Office软件,然后在C#项目中引用Microsoft.Office.Interop.Excel.dll文件。 读取Excel文件: ```csharp using Microsoft.Office.Interop.Excel; //打开Excel文件 Application excel = new Application(); Workbook workbook = excel.Workbooks.Open(filePath); //读取数据 Worksheet worksheet = workbook.Worksheets[1]; Range range = worksheet.UsedRange; int rowCount = range.Rows.Count; int colCount = range.Columns.Count; for (int i = 1; i <= rowCount; i++) { for (int j = 1; j <= colCount; j++) { string cellValue = range.Cells[i, j].Value2.ToString(); Console.Write(cellValue + "\t"); } Console.WriteLine(); } //关闭Excel文件 workbook.Close(false, Type.Missing, Type.Missing); excel.Quit(); ``` Excel文件: ```csharp using Microsoft.Office.Interop.Excel; //打开Excel文件 Application excel = new Application(); Workbook workbook = excel.Workbooks.Open(filePath); //入数据 Worksheet worksheet = workbook.Worksheets[1]; Range range = worksheet.Range["A1:C3"]; range.Value2 = new object[,] { { "A1", "B1", "C1" }, { "A2", "B2", "C2" }, { "A3", "B3", "C3" } }; //保存Excel文件 workbook.Save(); workbook.Close(); excel.Quit(); ``` 2. 使用第三方库读Excel文件 常用的第三方库有NPOI和EPPlus,它们都支持读Excel文件,而且不需要安装Microsoft Office软件。 使用NPOI读Excel文件: ```csharp using NPOI.HSSF.UserModel; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; //读取Excel文件 using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { IWorkbook workbook; if (filePath.EndsWith(".xls")) { workbook = new HSSFWorkbook(stream); } else if (filePath.EndsWith(".xlsx")) { workbook = new XSSFWorkbook(stream); } else { throw new Exception("Invalid Excel file format."); } ISheet sheet = workbook.GetSheetAt(0); int rowCount = sheet.LastRowNum + 1; for (int i = 0; i < rowCount; i++) { IRow row = sheet.GetRow(i); int colCount = row.LastCellNum; for (int j = 0; j < colCount; j++) { ICell cell = row.GetCell(j); string cellValue = cell.ToString(); Console.Write(cellValue + "\t"); } Console.WriteLine(); } } //Excel文件 using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { IWorkbook workbook = new XSSFWorkbook(); ISheet sheet = workbook.CreateSheet("Sheet1"); sheet.CreateRow(0).CreateCell(0).SetCellValue("A1"); sheet.CreateRow(1).CreateCell(0).SetCellValue("A2"); sheet.CreateRow(2).CreateCell(0).SetCellValue("A3"); sheet.CreateRow(3).CreateCell(0).SetCellValue("A4"); workbook.Write(stream); } ``` 使用EPPlus读Excel文件: ```csharp using OfficeOpenXml; //读取Excel文件 using (var package = new ExcelPackage(new FileInfo(filePath))) { ExcelWorksheet worksheet = package.Workbook.Worksheets[0]; int rowCount = worksheet.Dimension.Rows; int colCount = worksheet.Dimension.Columns; for (int i = 1; i <= rowCount; i++) { for (int j = 1; j <= colCount; j++) { string cellValue = worksheet.Cells[i, j].Value.ToString(); Console.Write(cellValue + "\t"); } Console.WriteLine(); } } //Excel文件 using (var package = new ExcelPackage(new FileInfo(filePath))) { ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet1"); worksheet.Cells["A1"].Value = "A1"; worksheet.Cells["A2"].Value = "A2"; worksheet.Cells["A3"].Value = "A3"; worksheet.Cells["A4"].Value = "A4"; package.Save(); } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值