こんなものがあったんですね。
Office Web Components 2003 (OWC)2000版もXP版もあるらしい。
これを使うと、簡単にExcelファイルを生成してダウンロードさせたり、
Excelのグラフを書き出して表示したり。。。
ってちっとも簡単じゃない。
Comオブジェクトだし。
VS2005で試したのですが、単純にインストールして「Microsoft Office Web Components 11.0」を参照設定すればOKっぽいです。(PCにはOffice 2007のみインストール状態)
で、あとは
ここの真似してC#で書いてみました。
- Response.Buffer = true;
- Response.ContentType = "image/gif";
-
- int[] aX = new int[10];
- int[] aY = new int[10];
-
- Random r = new Random();
-
- ChartSpace cs = new ChartSpace();
- ChChart chc = cs.Charts.Add(0);
- ChSeries chs = chc.SeriesCollection.Add(0);
- chs.Type = ChartChartTypeEnum.chChartTypeScatterLineMarkers;
- chs.SetData( ChartDimensionsEnum.chDimSeriesNames
- , (int)ChartSpecialDataSourcesEnum.chDataLiteral, "Chart1_Series1");
- chs.SetData(ChartDimensionsEnum.chDimXValues
- , (int)ChartSpecialDataSourcesEnum.chDataLiteral, aX);
- chs.SetData(ChartDimensionsEnum.chDimYValues
- , (int)ChartSpecialDataSourcesEnum.chDataLiteral, aY);
-
- chc.HasLegend = true;
- chc.Legend.Position = ChartLegendPositionEnum.chLegendPositionBottom;
- chc.HasTitle = true;
- chc.Title.Caption = "Chart1";
-
- chc.Axes[0].HasTitle = true;
- chc.Axes[0].Caption = "Y axis";
-
- chc.Axes[1].HasTitle = true;
- chc.Axes[1].Caption = "X axis";
-
-
- Response.BinaryWrite((byte[])cs.GetPicture("gif", 500, 400));
- Response.End();
Response.Buffer = true;
Response.ContentType = "image/gif";
int[] aX = new int[10];
int[] aY = new int[10];
Random r = new Random();
ChartSpace cs = new ChartSpace();
ChChart chc = cs.Charts.Add(0);
ChSeries chs = chc.SeriesCollection.Add(0);
chs.Type = ChartChartTypeEnum.chChartTypeScatterLineMarkers;
chs.SetData( ChartDimensionsEnum.chDimSeriesNames
, (int)ChartSpecialDataSourcesEnum.chDataLiteral, "Chart1_Series1");
chs.SetData(ChartDimensionsEnum.chDimXValues
, (int)ChartSpecialDataSourcesEnum.chDataLiteral, aX);
chs.SetData(ChartDimensionsEnum.chDimYValues
, (int)ChartSpecialDataSourcesEnum.chDataLiteral, aY);
chc.HasLegend = true;
chc.Legend.Position = ChartLegendPositionEnum.chLegendPositionBottom;
chc.HasTitle = true;
chc.Title.Caption = "Chart1";
chc.Axes[0].HasTitle = true;
chc.Axes[0].Caption = "Y axis";
chc.Axes[1].HasTitle = true;
chc.Axes[1].Caption = "X axis";
Response.BinaryWrite((byte[])cs.GetPicture("gif", 500, 400));
Response.End();
ReleaseComObjectとかはひとまず入れてないですが、必須ですよね。
っていってもうまく表示されないぞ。。。まぁいっか。気が向いたらもうちょっと見てみます。
あと、Excelを生成してダウンロードさせる方法が、
-
- SpreadsheetClass xlsheet = new SpreadsheetClass();
-
- for(int i = 1 ; i< 11 ; i++)
- {
- xlsheet.ActiveSheet.Cells[i, i] = i.ToString();
- }
- String xlFileName = System.DateTime.Now.Ticks.ToString() + ".xls";
- xlsheet.Export(Server.MapPath(".") + "\\" + xlFileName
- , SheetExportActionEnum.ssExportActionNone
- , SheetExportFormat.ssExportHTML);
- Response.ContentType = "application/x-msexcel";
- Response.WriteFile(Server.MapPath(".") + "\\" + xlFileName);
//Excelのダウンロード
SpreadsheetClass xlsheet = new SpreadsheetClass();
// ダミーデータ
for(int i = 1 ; i< 11 ; i++)
{
xlsheet.ActiveSheet.Cells[i, i] = i.ToString();
}
String xlFileName = System.DateTime.Now.Ticks.ToString() + ".xls";
xlsheet.Export(Server.MapPath(".") + "\\" + xlFileName
, SheetExportActionEnum.ssExportActionNone
, SheetExportFormat.ssExportHTML);
Response.ContentType = "application/x-msexcel";
Response.WriteFile(Server.MapPath(".") + "\\" + xlFileName);
こっちは使い道があるかな。