C#+Aspose.Cells Excelエクスポートおよび設定スタイル(Webform/Winform)
7760 ワード
プロジェクトで使用します.ここに記録します.Cellsはマシンに依存せずEXCELを装着していなくてもエクスポートできるので便利です.具体的には他の
http://www.aspose.com/docs/display/cellsnet/Importing+Data+to+Worksheets#ImportingDatatoWorksheets-array
Webfromバージョン:
効果は次のとおりです.
Winformバージョン:
効果図は以下の通りです.
使用方法:ExportExcelWithAspose(SaveExcelData,「バロックバーコードデータ一覧」);//SaveExcelDataはdatatable
http://www.aspose.com/docs/display/cellsnet/Importing+Data+to+Worksheets#ImportingDatatoWorksheets-array
Webfromバージョン:
効果は次のとおりです.
protected void btnAsnExport_ServerClick(object sender, EventArgs e)
{
var getAsnData = SearchDataClass.GetAsnSearchData(txtAsnNo.Value,
hfCustomerID.Value, txtTimeSelect.Value,txtSku.Value,txtSkuContent.Value);
// excel
ArrayList ColTitle = new ArrayList()
{ "ASN ", "SKU", " ", " ", " ",
" "," "," "," ","ASN ","ASN " };
//string[] strTitle = new string[] { "ASNNo", "SKU", "SKUDescrC", "ExpectedQty", "ReceivedQty", "UOM",
"ReceivingLocation", "ReceivedTime", "CustomerID", "CodeName_C" };
if (getAsnData.ToList().Count > 0)
{
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
// sheet
Aspose.Cells.Worksheet sheet = workbook.Worksheets[0];
//
Aspose.Cells.Style style = workbook.Styles[workbook.Styles.Add()];
style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;
style.Borders[Aspose.Cells.BorderType.LeftBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //
style.Borders[Aspose.Cells.BorderType.RightBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //
style.Borders[Aspose.Cells.BorderType.TopBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //
style.Borders[Aspose.Cells.BorderType.BottomBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //
// PutValue
int currow = 0;
byte curcol = 0;
//sheet.Cells.ImportCustomObjects((System.Collections.ICollection)getAsnData,
//strTitle, true, 0, 0, getAsnData.Count, true, "yyyy/MM/dd HH:mm", false);
sheet.Cells.ImportCustomObjects((System.Collections.ICollection)getAsnData,
null, true, 0, 0, getAsnData.Count, true, "yyyy/MM/dd HH:mm", false);
//
for (int i = 0; i < getAsnData.ToList().Count; i++)
{
for (int j = 0; j < 11; j++)
{
sheet.Cells[i + 1, j].Style = style;
sheet.Cells[i + 1, 2].Style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Left;
sheet.Cells[i + 1, 7].Style.Custom = "yyyy/MM/dd HH:mm";
sheet.Cells[i + 1, 10].Style.Custom = "yyyy/MM/dd HH:mm";
}
}
//
foreach (string s in ColTitle)
{
sheet.Cells[currow, curcol].PutValue(s);
style.ForegroundColor = System.Drawing.Color.FromArgb(153, 204, 0);
style.Pattern = Aspose.Cells.BackgroundType.Solid;
style.Font.IsBold = true;
sheet.Cells[currow, curcol].Style = style;
curcol++;
}
Aspose.Cells.Cells cells = sheet.Cells;
//
cells.SetRowHeight(0, 30);
//
sheet.AutoFitColumns();
//
System.IO.MemoryStream ms = workbook.SaveToStream();
byte[] bt = ms.ToArray();
//
string fileName = " " + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
//
Response.ContentType = "application/vnd.ms-excel";
//
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
Response.BinaryWrite(bt);
Response.Flush();
Response.End();
}
}
Winformバージョン:
効果図は以下の通りです.
public void ExportExcelWithAspose(MDataTable dt, string fileName)
{
string saveFileName = "";
SaveFileDialog saveDialog = new SaveFileDialog();
saveDialog.DefaultExt = "xls";
saveDialog.Filter = "Excel |*.xls";
saveDialog.FileName = fileName;
saveDialog.ShowDialog();
saveFileName = saveDialog.FileName;
if (saveFileName.IndexOf(":") < 0) return; //
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
if (xlApp == null)
{
MessageBox.Show(" Excel , Excel");
return;
}
try
{
Microsoft.Office.Interop.Excel.Workbooks workbooks = xlApp.Workbooks;
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
Aspose.Cells.Worksheet cellSheet = workbook.Worksheets[0];
Aspose.Cells.Cells cells = cellSheet.Cells ;//
//
Aspose.Cells.Style style = workbook.Styles[workbook.Styles.Add()];
//
style.HorizontalAlignment = Aspose.Cells.TextAlignmentType.Center;
//
int rowIndex = 0;
//
int colIndex = 0;
//
int colCount = dt.Columns.Count;
//
int rowCount = dt.Rows.Count;
rowIndex++;
for (int i = 0; i < rowCount; i++)
{
colIndex = 0;
for (int j = 0; j < colCount; j++)
{
if (j == 5) { cellSheet.Cells[rowIndex, colIndex].PutValue(Convert.ToDateTime (dt.Rows[i][j].Value).ToString("yyyy/MM/dd HH:mm:ss")); }
else { cellSheet.Cells[rowIndex, colIndex].PutValue(dt.Rows[i][j].Value); }
style.Borders[Aspose.Cells.BorderType.LeftBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //
style.Borders[Aspose.Cells.BorderType.RightBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //
style.Borders[Aspose.Cells.BorderType.TopBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //
style.Borders[Aspose.Cells.BorderType.BottomBorder].LineStyle = Aspose.Cells.CellBorderType.Thin; //
cellSheet.Cells[rowIndex, colIndex].Style = style;
colIndex++;
}
rowIndex++;
}
// 0
rowIndex = 0; colIndex = 0;
//
for (int i = 0; i < colCount; i++)
{
cellSheet.Cells[rowIndex, colIndex].PutValue(dt.Columns[i].ColumnName);
//
style.ForegroundColor = System.Drawing.Color.FromArgb(153, 204, 0);
style.Pattern = Aspose.Cells.BackgroundType.Solid;
style.Font.IsBold = true;
style.IsTextWrapped = true;
cells.SetRowHeight(0, 38);//
cellSheet.Cells[rowIndex, colIndex].Style = style;
colIndex++;
}
cellSheet.AutoFitColumns();
workbook.Save(Path.GetFullPath(saveFileName));
xlApp.Quit();
GC.Collect();//
MessageBox.Show(" : " + fileName + ".xls ", " ", MessageBoxButtons.OK,MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show(" , !
" + ex.Message);
}
}
使用方法:ExportExcelWithAspose(SaveExcelData,「バロックバーコードデータ一覧」);//SaveExcelDataはdatatable