Aspose.Cells导入导出excel文件
导入
Workbook workbook = new Workbook(openFileDialog1.FileName); //实例化Workbook
Worksheet sheet = workbook.Worksheets[0]; //实例化Worksheet
Cells cells = sheet.Cells;
Worksheet sheet = workbook.Worksheets[0]; //实例化Worksheet
Cells cells = sheet.Cells;
DataTable dt = cells.ExportDataTableAsString(0, 0, cells.MaxDataRow + 1, cells.MaxColumn + 1, true); //转成DataTable
导出
DataTable dt = (DataTable)GridControl3.DataSource;
foreach( DataRow dr in dt.Rows)
{
dr["cPartInfo"]=Convert.ToString(dr["cPartInfo"]).Replace(@"\r\n","\n"); //换行用"\n"
dr.EndEdit();
}
foreach( DataRow dr in dt.Rows)
{
dr["cPartInfo"]=Convert.ToString(dr["cPartInfo"]).Replace(@"\r\n","\n"); //换行用"\n"
dr.EndEdit();
}
Workbook workbook = new Workbook(); //工作簿
Worksheet sheet = workbook.Worksheets[0]; //工作表
Cells cells = sheet.Cells;//单元格
Worksheet sheet = workbook.Worksheets[0]; //工作表
Cells cells = sheet.Cells;//单元格
//数据样式
Style style2 = workbook.Styles[workbook.Styles.Add()];//新增样式
style2.HorizontalAlignment = TextAlignmentType.Center;//文字居中
style2.Font.Name = "微软雅黑";//文字字体
style2.Font.Size = 9;//文字大小
style2.IsTextWrapped = true;//单元格内容自动换行
style2.HorizontalAlignment = TextAlignmentType.Center;//文字居中
style2.Font.Name = "微软雅黑";//文字字体
style2.Font.Size = 9;//文字大小
style2.IsTextWrapped = true;//单元格内容自动换行
style2.Borders[BorderType.LeftBorder].LineStyle = CellBorderType.Thin;
style2.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
style2.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
style2.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
style2.Borders[BorderType.RightBorder].LineStyle = CellBorderType.Thin;
style2.Borders[BorderType.TopBorder].LineStyle = CellBorderType.Thin;
style2.Borders[BorderType.BottomBorder].LineStyle = CellBorderType.Thin;
sheet.Cells.SetColumnWidth(0, 20); //设置列宽
sheet.Cells.SetColumnWidth(1, 25);
sheet.Cells.SetColumnWidth(2, 9);
sheet.Cells.SetColumnWidth(3, 9);
sheet.Cells.SetColumnWidth(5, 8);
sheet.Cells.SetColumnWidth(1, 25);
sheet.Cells.SetColumnWidth(2, 9);
sheet.Cells.SetColumnWidth(3, 9);
sheet.Cells.SetColumnWidth(5, 8);
Range range = sheet.Cells.CreateRange(0, 0, 1, 16); //设置样式
range.SetStyle(style2);
range.SetStyle(style2);
range.Merge(); //合并
cells.Merge(0, 0, 2, 0); //合并
//单元格赋值
sheet.Cells[0, 0].PutValue("value1");
//插入图片
WebClient objwebClient = new WebClient();
MemoryStream ms= new MemoryStream(objwebClient.DownloadData(cImagePath));
int pictureIndex = sheet.Pictures.Add(i+1, 0, ms);
Picture pic = sheet.Pictures[pictureIndex];
pic.Height = 65;
pic.Width = 120;
pic.Left = 10;
pic.Top =10+ (i+1)*6;
MemoryStream ms= new MemoryStream(objwebClient.DownloadData(cImagePath));
int pictureIndex = sheet.Pictures.Add(i+1, 0, ms);
Picture pic = sheet.Pictures[pictureIndex];
pic.Height = 65;
pic.Width = 120;
pic.Left = 10;
pic.Top =10+ (i+1)*6;
//保存
workbook.Save(SaveFileDialog.FileName);

浙公网安备 33010602011771号