1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
| using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
namespace OpenAndReadExcel
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
Excel.Application appExl;
Excel.Workbook workbook;
Excel.Worksheet NwSheet;
Excel.Range ShtRange;
appExl = new Excel.ApplicationClass();
workbook = appExl.Workbooks.Open(Server.MapPath("Global EPT P1&P2 11082011.xlsx"), Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);
NwSheet = (Excel.Worksheet)workbook.Sheets.get_Item(1);
int Cnum = 0;
int Rnum = 0;
String Locations = "";
ShtRange = NwSheet.UsedRange;
DataTable dt = new DataTable();
dt.Columns.Add("Site Name");
dt.Columns.Add("Longitude");
dt.Columns.Add("Latitude");
for (Rnum = 2; Rnum <= ShtRange.Rows.Count; Rnum++)
{
DataRow dr = dt.NewRow();
int f = 1;
for (Cnum = 1; Cnum <= 11; Cnum++)
{
if ((ShtRange.Cells[1, Cnum] as Excel.Range).Value2.ToString() == "Site Name" || (ShtRange.Cells[1, Cnum] as Excel.Range).Value2.ToString() == "Latitude" || (ShtRange.Cells[1, Cnum] as Excel.Range).Value2.ToString() == "Longitude")
{
dr[f - 1] = (ShtRange.Cells[Rnum, Cnum] as Excel.Range).Value2.ToString();
f++;
}
}
dt.Rows.Add(dr);
dt.AcceptChanges();
}
Session["data"] = dt;
foreach (DataRow r in dt.Rows)
{
// bypass empty rows
// if (r["Latitude"].ToString().Trim().Length == 0)
// continue;
var Latitude = r["Latitude"].ToString();
var Longitude = r["Longitude"].ToString();
var Site = r["Site Name"].ToString();
//HtmlLink canonical = new HtmlLink();
// canonical.Href = "http://example.com/Content/Default.aspx";
// canonical.Href = "http://Default.aspx";
var myHtml = "<b>" + Site + "</b><br /><a href='Default.aspx' style='color:#00303f;font:bold 12px verdana;' title='click to contact'>More details</a></div>";
// System.Diagnostics.Debug.Write(myHtml);
// create a line of JavaScript for marker on map for this record
Locations += Environment.NewLine + " latlng = new GLatLng(" + Latitude + " , " + Longitude + ")" + Environment.NewLine + "map.addOverlay(createMarker1(latlng," +myHtml + "));";
//+ Environment.NewLine + "var greenIcon = new GIcon(G_DEFAULT_ICON);" + Environment.NewLine + "greenIcon.image = 'http://www.google.com/intl/en_us/mapfiles/ms/micons/green-dot.png';"
// + Environment.NewLine + " markerOptions = { icon: greenIcon };"
// + Environment.NewLine + " var marker = new GMarker(latlng, markerOptions);"
// "GEvent.addListener(marker, 'click', function() { map.openInfoWindowHtml(latlng , " + myHtml + ");});"
// + Environment.NewLine + "marker.bindInfoWindowHtml("+myHtml+");"
// + Environment.NewLine + "map.addOverlay(marker);";
}
workbook.Close(true, Missing.Value, Missing.Value);
appExl.Quit();
// Session["data"] = dt;
js.Text = @"
<script type='text/javascript'>
function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById('map_canvas'));
map.setCenter(new GLatLng(45.05, 7.6667), 2);
function createMarker1(latlng, myHtml) {
var redIcon = new GIcon(G_DEFAULT_ICON);
redIcon.image = 'http://mrant.net/wp3/wp-content/uploads/2011/05/Google_Maps_Marker.png';
markerOptions = { icon: redIcon };
var marker = new GMarker(latlng, markerOptions);
GEvent.addListener(marker, 'click', function () {
map.openInfoWindowHtml(latlng, myHtml );
});
return marker;
}
var bounds = map.getBounds();
var southWest = bounds.getSouthWest();
var northEast = bounds.getNorthEast();
var lngSpan = northEast.lng() - southWest.lng();
var latSpan = northEast.lat() - southWest.lat();
var latlng;
" + Locations + @"
map.setUIToDefault();
map.setMapType(G_HYBRID_MAP);
}
}
</script> ";
}
}
}
} |
Partager